Python Set

What is set?

Set is one of the built-in data type of python. Sets are container used to store collection of different data types in a single variable. It is always enclosed with curly-bracket.


You can also change other data type into set.


Set Properties

Following are the properties of set :

  • Unordered : Set items are unordered.
  • Unchangeable : Set items are unchangeable means items cannot be added or removed directly from the set.

  • Not Allow Duplicate : Set items cannot be used multiple time in the same set.
  • Unindexed : Set items cannot be sliced or indexed.


Set Methods

Python set methods helps to manipulate the set. Following are the built-in methods of set :

  •  set.copy()  : Used to copy a set or create a duplicate set.
  • set.add()  : Used to add a specific element in the set.



  •  set.discard()  : Used to remove a specific item from the set.
  •  set.pop()  : Used to remove first item from the set.
  •  set.remove()  : Used to remove a specific item from the set.
  •  set.clear()  : Used to remove all the items from and create a empty set.


  •  set.union()  : Used to create a set from the union of sets.
  •  set.intersection()  : Used to create a set from the intersection of sets.



Is there any useful methods left in this list? Let me know in the comment section.

Comments