Python Sets

According to our experience, creators of Python have tried to make the sets in Python similar to the sets which are in maths so that it would be easy to understand them and this will be an advantage for you if you have studied sets before.

Sets are "unordered collection" of elements and are defined within "curly brackets {}" and the elements are separated with a comma(,).

Sets are "mutable" datatypes i.e. we can modify them after their creation.

Example

mySet = {1, 2, 3, 4, 5}
yourSet = {"This", "is", "your", "set"}
print(mySet)
print(yourSet)

Output

{1, 2, 3, 4, 5}
{'is', 'set', 'your', 'This'}

Sets contain single elements i.e. there are no duplicate elements.

We are free to declare sets with duplicate elements but Python will count only one time with no errors.

Example

mySet = {1, 2, 3, 4, 5, 1, 2, 3}
print(mySet)

Output

{1, 2, 3, 4, 5}

As sets are unordered so the index of any element can not be known in advance i.e. one element can have different or same index values at different times.

Example

mySet = {1, 2, 3, 4, 5}
yourSet = {"This", "is", "your", "set"}
print(mySet)
print(yourSet)

Output(different times)

{1, 2, 3, 4, 5}
{'set', 'is', 'your', 'This'}

{1, 2, 3, 4, 5}
{'This', 'is', 'set', 'your'}

{1, 2, 3, 4, 5}
{'set', 'is', 'This', 'your'}

Check an element in the set

We can check whether an element is present in a set or not.

We can use classical methods like using for loop or while loop and running it until the element doesn’t match with any element in the set.

But, here we will use "in" keyword to check the existence of an element in a set.

Example

mySet = {1, 2, 3, 4, 5}
print(1 in mySet) # will return True if present else False
print(5 in mySet)
print(6 in mySet)
print(100 in mySet)

Output

True
True
False
False

Updating sets

We can add more items to a set using "add()" and "update()".

"add()" is used to add one element and "update()" is used to add any number of elements.

Example

mySet = {1, 2, 3, 4, 5}
mySet.add(6)
print(mySet)
mySet.update([7, 8, 9])
print(mySet)

Output

{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6, 7, 8, 9}

set() method

We can use "set()" method to create sets.

Example

myString = "I am a String"
myList = [1, 2, 3, 4]
myTuple = (5, 6, 7, 8)
set1 = set(myString)
set2 = set(myList)
set3 = set(myTuple)
print(set1)
print(set2)
print(set3)

Output

{'r', 'i', 'n', 'S', 'g', ' ', 'I', 'a', 'm', 't'}
{1, 2, 3, 4}
{8, 5, 6, 7}

len()

Using "len()", we can find out the length of sets i.e. the number of elements present in a set.

Example

set1 = {1, 2, 3, 4, 5}
set2 = {"I", "am", "a", "set"}
print(len(set1))
print(len(set2))

Output

5
4

clear() and del

Use "clear()" to clear the elements of a set i.e. to delete the elements inside a set.
Use "del" keyword to delete a set permanently.

Example

mySet = {1, 2, 3, 4, 5}
mySet.clear()
print(mySet)
del mySet
print(mySet)

Output

set()
Traceback (most recent call last):
  File "f:/vscode/python/fsggr.py", line 5, in <module>
    print(mySet)
NameError: name 'mySet' is not defined

remove() and discard()

Use "remove()"and"discard()"to remove an element from a set.

"remove()"will raise an error if the element we are removing does not exist in the set.

"discard()"will not raise any error if the element does not exist in the set.

Example

mySet = {1, 2, 3, 4, 5}
mySet.remove(1)
print(mySet)
mySet.discard(2)
print(mySet)

Output

{2, 3, 4, 5}
{3, 4, 5}

pop() method

We can use "pop()"method to remove the last element from a set.

But it will not be possible for you to know that which element will be removed from the set, because you know that sets are unordered.

"pop()" will return that value that has been removed from the set using the pop() method.

Example

mySet = {1, 2, 3, 4, 5}
returnValue = mySet.pop()
print(mySet)
print(returnValue)

Output

{2, 3, 4, 5}
1

Useful Methods


MethodDescription
union()Return a set containing the union of sets
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
intersection()Returns a set, that is the intersection of two other sets
isdisjoint()Returns True if two sets contain similar elements
issubset()Returns True if the set is contained in the set
issuperset()Returns True if the set contains another set
symmetric_difference()Returns a set with the symmetric differences of two sets