Sets in Python
FREE Online Courses: Your Passport to Excellence - Start Now
You would have heard of sets and set theory in Mathematics. Sets in Python are similar to those in math.
In this article, we will learn sets in Python, accessing elements, modifying them, using functions, operators, methods, and so on. So let’s not wait and begin with the introduction.
Sets in Python
A set is an unordered and mutable collection of unique elements. It works based on another data structure called a hash table. Because of this, it is advantageous over the lists while checking for an element in a set.
Creating a set in Python
Sets have the elements in the curly brackets with each of them separated by a comma. It can contain values of different data types.
Example of creating a set:
set1={1,2,3,4,5} #creating a set with same data type print(f"The type of {set1} is {type(set1)}") set2={3.4,'t','PythonGeeks',4} #creating a set with different data type print(f"The type of {set2} is {type(set2)}")
Output:
The type of {1, 2, 3, 4, 5} is <class ‘set’>
The type of {3.4, ‘PythonGeeks’, 4, ‘t’} is <class ‘set’>
1. Does not allow duplicates:
One of the properties of a set is not to allow duplicate values. While creating a set if we give a duplicate value, it will occur only once in the set. For example,
Example of creating a set with duplicate values:
set_1={1,3,'a',5,'a',3,7,3} print(set_1)
Output:
2. Unhashable elements:
Though a set is mutable, it cannot have mutable elements like lists, sets, or dictionaries. Therefore, there is no concept of nested sets. So we get an error if we try to create a set with any of these data types as shown below.
Example of creating a set with mutable elements :
set1={1,[3,'b',5.0],6.7,'t'} set2={4.6,{5,6,'t'},7} set3={3.2,{1:'s',2:'r'},7,'y'}
Output:
File “<pyshell#6>”, line 1, in <module>
set1={1,[3,’b’,5.0],6.7,’t’}
TypeError: unhashable type: ‘list’
File “<pyshell#7>”, line 1, in <module>
set2={4.6,{5,6,’t’},7}
TypeError: unhashable type: ‘set’
File “<pyshell#8>”, line 1, in <module>
set3={3.2,{1:’s’,2:’r’},7,’y’}
TypeError: unhashable type: ‘dict’
But we can include a tuple in a set as it is immutable. For example,
Example of creating a set with a tuple:
set4={1,3.6,(4,'t',7.8)} set4
Output:
3. Creating an empty set:
Generally, when we try creating an empty list or tuple or a dictionary, we give only the corresponding brackets. But this is not possible in the case of an empty set. Look at the below example to know the reason.
Example of creating a variable with curly brackets:
var={} print(f"The type of {var} is {type(var)}")
Output:
Remember even the dictionaries use curly brackets to enclose their elements. So, when we try to give empty curly brackets, it is considered as an empty dictionary.
Then how do we create an empty set? You will find the answer for this in the next point where we create a set using the set() function.
4. Creating set using the set() function:
Another way to create a set is by using the set() function. First let us see the creation of an empty set, which is false in a boolean context.
Example of creating an empty set using the set() function:
set1=set() print(f"The type of {set1} is {type(set1)}") bool(set1)
Output:
The type of set() is <class ‘set’>
False
We can give any iterable as an argument to the set(), to create the corresponding set.
Example of creating a set using the set() function:
set1=set("PythonGeeks") #creating a set from a string print(set1) set2=set([1,2,3,4,1,2]) #creating a set from a list set2 set3=set(('a','b',4,5.6))#creating a set from a tuple set3
Output:
{‘P’, ‘h’, ‘n’, ‘o’, ‘y’, ‘e’, ‘k’, ‘t’, ‘G’, ‘s’}
{1, 2, 3, 4}
{4, 5.6, ‘b’, ‘a’}
Observe that the duplicates from the arguments of the set do not occur in the resulting set. Also, see the difference between the creation of a set using curly brackets and using the set() function with a single string.
Example of creating a set with string:
set1={'abab'} set1 set2=set('abab') set2
Output:
{‘abab’}
{‘b’, ‘a’}
Interesting result, isn’t it? This is because when we create using the bracket, the string is considered as a single value. But while using the set() function, it is considering the string as an iterable with a collection of characters.
Accessing the values of sets in python
Since sets are not ordered, they do not support indexing. However, we can access the entire set using the set name as shown below.
Example of accessing the entire set:
set1={1,2,'r','y',6.9} set1 set2={1,4,2,1,3,2} set2
Output:
{‘r’, 1, 2, ‘y’, 6.9}
{1, 2, 3, 4}
Observe the set 2 results. We can see that the duplicates got removed and the elements got ordered.
Let us not keep the doubt of indexing and check that.
Example of accessing element of a set using indexing:
set1={5.6,'g',8,4,'k'} set1[4]
Output:
File “<pyshell#38>”, line 1, in <module>
set1[4]
TypeError: ‘set’ object is not subscriptable
We can observe from the error that we cannot use indexing for the sets, which means indexing is not allowed.
Adding Elements and Updating a Set in Python
We just saw above that the set does not support indexing. For this reason, we cannot use an index for reassigning a value of a set. But don’t worry, because we have concerned functions to update and add values. We will discuss these functions in this section.
1. Python add():
We can use this function to add a value to the set. For example,
Example of adding a value to a set using add():
set1={3.4,1,6,5.0} set1 set1.add(2) set1
Output:
{1, 3.4, 5.0, 6}
{1, 2, 3.4, 5.0, 6}
We can see that on adding element 2, the set got rearranged to maintain the order. If the added element is less than all the elements of the set, then all the elements have to be shifted to the right. This makes the complexity O(n) (Order of n). But the average case can be taken as O(1).
However, the set does not get affected when we try adding an element of the set.
Example of adding an existing value to a set using add():
set1={3.4,1,6,5.0} set1 set1.add(1) set1
Output:
{1, 3.4, 5.0, 6}
{1, 3.4, 5.0, 6}
2. Python update():
We use this function generally to add multiple elements to the set. We can give these elements lists or tuples too. If we give them as normal data types we get an error. As usual, if we add duplicates, they are ignored.
Example of adding values to a set using update():
set1={1,'r',5.7,'u'} set1.update([1,'j'],{'r','h',9.0}) set1 set1.update(4,2,'r')
Output:
{1, ‘h’, 5.7, ‘u’, ‘j’, 9.0, ‘r’}
Traceback (most recent call last):
File “<pyshell#54>”, line 1, in <module>
set1.update(4,2,’r’)
TypeError: ‘int’ object is not iterable
Deleting values from a list in Python
Again we have a couple of in-built functions to delete the values of a set and we cannot use indexing.
1. Python discard():
This function deletes the value given as an argument. It doesn’t give an error if the argument is not present in the set, it just keeps the set intact.
Example of deleting values to a set using discard():
set1={'a','b','c','d','e'} set1 set1.discard('b') set1 set1.discard('t') set1
Output:
{‘c’, ‘d’, ‘e’, ‘a’, ‘b’}
{‘c’, ‘d’, ‘e’, ‘a’}
{‘c’, ‘d’, ‘e’, ‘a’}
2. Python remove():
This function is similar to discard and deletes the value given as an argument. The only difference is that it gives an error if the argument is not present in the set.
Example of deleting values to a set using remove():
set1={'a','b','c','d','e'} set1 set1.remove('e') set1 set1.remove('h')
Output:
{‘c’, ‘d’, ‘e’, ‘a’, ‘b’}
{‘c’, ‘d’, ‘a’, ‘b’}
Traceback (most recent call last):
File “<pyshell#69>”, line 1, in <module>
set1.remove(‘h’)
KeyError: ‘h’
3. Python pop():
This function pops a random element from the set. Since we cannot index in the case of sets, this function does not take any argument.
Example of deleting values to a set using pop():
set1={'a','b','c','d','e'} set1 set1.pop() set1.pop()
Output:
{‘c’, ‘d’, ‘e’, ‘a’, ‘b’}
‘c’
‘d’
4. Python clear():
This function removes all the values of a dictionary. But it keeps the instance of the set and when called gives an empty set.
Example of deleting values to a set using clear():
set1={'a','b','c','d','e'} set1 set1.clear() set1
Output:
set()
Functions on sets in Python
There are many functions on sets that we can apply to get some information from the set. We will discuss them in this section.
1. Python len():
The function len() finds the length of the set, that is, the number of elements of a set. For an empty set, the length is 0.
Example of using the len():
set1=set() len(set1) set2={8,5.1,'gfd',54} set2={8,5.1,'gfd',54,(0,1)} len(set2)
Output:
0
5
2. Python max():
The function max() finds the largest element/ the maximum of the set if all the elements belong to the same category of values.
If they don’t for example if the set has a mixture of strings and numbers. And if we try to find the max, we get an error as strings and numbers cannot be compared.
Example of finding the max():
set1={4,5,2.7,9.0} max(set1) set2={'abc',5.9,3,8,'xyz'} max(set2)
Output:
9.0
Traceback (most recent call last):
File “<pyshell#85>”, line 1, in <module>
max(set2)
TypeError: ‘>’ not supported between instances of ‘int’ and ‘str’
3. Python min():
The function min() finds the smallest element/ the minimum of the set if all the elements belong to the same category of values.
If they don’t belong to the same type, for example, if the set has a mixture of strings and numbers. And if we try to find the max, we get an error as strings and numbers cannot be compared.
Example of finding the max():
set1={4,5,2.7,9.0} min(set1) set2={'abc',5.9,3,8,'xyz'} min(set2) set3={'a','b','c','d','e'} min(set3)
Output:
2.7
Traceback (most recent call last):
File “<pyshell#89>”, line 1, in <module>
min(set2)
TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’
‘a’
4. Python sum():
The function min() finds the smallest element/ the minimum of the set if all the elements are numbers.
If they don’t belong to the other data type, like strings, we get an error as strings cannot be summed.
Example of finding the sum():
set1={4,15,-2.3,0.7} sum(set1) set2={'a','b','c','d','e'} sum(set2)
Output:
17.4
Traceback (most recent call last):
File “<pyshell#98>”, line 1, in <module>
sum(set2)
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
5. Python all():
This function checks if all the elements of the set are not empty or 0 or False and returns either True or False. It returns False if any of the elements is empty or 0 or False. It can work with a set of varied data types.
Example of using the all() function on a set:
set1={1,3,'',0} all(set1) set2={'abc',5.9,3,8,'xyz'} all(set2)
Output:
False
True
6. Python any():
This function checks if any of the elements of the set are not empty or 0 pr zero. And returns either True or False. It returns False only if all of the elements are empty or 0 or False. It can work on any tuple.
Example of using the any() function on a set:
set1={1,3,'',0} any(set1) set2={'',0,False} any(set2)
Output:
True
False
7. Python sorted():
This function returns the version of the set in the form of a list with all elements arranged in increasing order if all values belong to the same data type. This function does not affect the order of elements in the tuple, it only returns the arranged version.
If we try to apply the sorted() on a set with different data types, we will get an error. This is because the values of different data types cannot be compared.
Example of using the sorted() function on a set:
set1={4,5,-2.7,9.0} set1 sorted(set1) set1 set2={'abc',5.9,3,8,'xyz'} sorted(set2)
Output:
{9.0, 4, 5, -2.7}
[-2.7, 4, 5, 9.0]
{9.0, 4, 5, -2.7}
Traceback (most recent call last):
File “<pyshell#115>”, line 1, in <module>
sorted(set2)
TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’
8. Python enumerate():
This function returns the enumerated object of the set and contains the value and index of the set as a pair. These can work on a set with aby data type elements.
Example of using the enumerate() function on a set:
set1={'abc',5.9,3,8,'xyz'} enm=enumerate(set1) print(enm,":",type(enm)) print(set(enm))
Output:
<enumerate object at 0x0000021DF379D2C0> : <class ‘enumerate’>
{(2, 3), (0, ‘abc’), (1, ‘xyz’), (4, 8), (3, 5.9)}
Methods on Sets in Python
We have discussed some methods in the sections on adding, updating, and deleting values in the set. Besides these, we have different methods we can perform on the set. We will cover them in the section.
1. Python union():
This method returns the values of both sets, ignoring the duplicates. The union() is similar to doing the or (|) operation on the sets. Time Complexity is O(len(set1) + len(set2)) where set1 and set2 are the two sets whose union is done.
Example of doing the union() on two sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7.8} print("set1|set2:",set1|set2) print("set1.union(set2):",set1.union(set2))
Output:
set1|set2: {‘6’, 4, 5, 7, 7.8, ‘r’, ‘PythonGeeks’, ‘l’}
set1.union(set2): {‘6’, 4, 5, 7, 7.8, ‘r’, ‘PythonGeeks’, ‘l’}
We can do these with more than one set too, by giving more than one set as the arguments to the function or by using more than one set operated by ‘or’.
Example of doing the union() on multiple sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7.8} set3={'a','b','c','d','e'} set1.union(set2,set3) set1|set2|set3
Output:
{‘c’, ‘6’, 4, 5, ‘d’, 7, 7.8, ‘a’, ‘r’, ‘PythonGeeks’, ‘e’, ‘b’, ‘l’}
{‘c’, ‘6’, 4, 5, ‘d’, 7, 7.8, ‘a’, ‘r’, ‘PythonGeeks’, ‘e’, ‘b’, ‘l’}
The only difference is that we can use the union() to compare a set and a tuple. But this is not possible with the or operation.
Example of doing the union() on a set and a tuple:
set1={4,'l',5,7} set1 | (1,2,3,4) set1.union((1,2,3,4))
Output:
File “<pyshell#131>”, line 1, in <module>
set1 | (1,2,3,4)
TypeError: unsupported operand type(s) for |: ‘set’ and ‘tuple’
2. intersection() in Python:
This method returns the values common to both sets. The intersection() is similar to doing the and (&) operation on the sets. Time Complexity is O(min(len(set1) , len(set2))) where set1 and set2 are the two sets whose union is done.
Example of doing the intersection() on sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} print("set1&set2:",set1&set2) print("set1.intersection(set2):",set1.intersection(set2))
Output:
set1&set2: {7}
set1.intersection(set2): {7}
Similar to the union(), we can use an intersection with a tuple and a dictionary. We can also use it on multiple sets as shown below.
Example of doing the intersection() on multiple sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} set3={'a','b','c','d','e'} set1.intersection(set2,set3) set1&set2&set3
Output:
set()
set()
3. difference() in Python:
This method returns the values in the first set that are not there in the second one. The intersection() is similar to doing the minus(-) operation on the sets. Time Complexity is O(len(set1)) where set1 and set2 are the two sets whose union is done.
Example of doing the difference() on a sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} print("set1.difference(set2):",set1.difference(set2)) print("set1-set2:",set1-set2)
Output:
set1.difference(set2): {4, 5, ‘l’}
set1-set2: {4, 5, ‘l’}
We can use an intersection with a tuple and a dictionary. We can also use it on multiple sets as shown below.
Example of doing the difference() on multiple sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} set3={'a','b','c','d','e'} set1.difference(set2,set3) set1-set2-set3
Output:
{4, 5, ‘l’}
{4, 5, ‘l’}
4. symmetric_difference() in python:
This method returns the values that are not common to both sets. The intersection() is similar to doing the xor(^) operation on the sets.
Example of doing the symmetric_difference() on sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} print("set1.symmetric_difference(set2):",set1.symmetric_difference(set2)) print("set1^set2:",set1^set2)
Output:
set1.symmetric_difference(set2): {‘r’, ‘6’, ‘PythonGeeks’, 4, 5, 8, ‘l’}
set1^set2: {‘r’, ‘6’, ‘PythonGeeks’, 4, 5, 8, ‘l’}
We can use an intersection with a tuple and a dictionary. We cannot also use it on multiple sets with the symmetric_difference(), but we can use it with the ^ operator as shown below.
Example of doing the symmetric_difference() on multiple sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} set3={'a','b','c','d','e'} set1.symmetric_difference(set2,set3) set1^set2^set3
Output:
File “<pyshell#153>”, line 1, in <module>
set1.symmetric_difference(set2,set3)
TypeError: set.symmetric_difference() takes exactly one argument (2 given)
{4, 5, 8, ‘r’, ‘PythonGeeks’, ‘c’, ‘6’, ‘d’, ‘a’, ‘e’, ‘b’, ‘l’}
5. isdisjoint() in Python:
This function True is sets have no common elements else False. It is again similar to & operator, where we check if we get an empty set on doing this operation.
Example of doing the isdisjoint() on sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} set1.isdisjoint(set2) set3={'x','y',1} set1.isdisjoint(set3)
Output:
False
True
This function accepts only one argument. So, we cannot compare more than 2 sets. We get an error if we try doing so as shown below.
Example of doing the isdisjoint() on multiple sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} set3={'x','y',1} set1.isdisjoint(set2,set3)
Output:
File “<pyshell#159>”, line 1, in <module>
set1.isdisjoint(set2,set3)
TypeError: set.isdisjoint() takes exactly one argument (2 given)
6. issubset() in Python:
This function is True if the set is the subset of the one in the brackets, that is the set in the bracket has all the elements of the other one. This is similar to checking with the operator ‘<=’. If this condition is satisfied then set2 is said to be the subset of set1.
If the set2 and set2 also satisfy the condition set2<set1, then the set2 is called the proper subset of set1. For example,
Example of doing the issubset() on sets:
set1={1,2,3,4,5} set2={1,3,4} set2.issubset(set1) set2<=set1 set3={1,3,6} set3.issubset(set1) set3<=set1
Output:
True
True
False
False
7. issuperset() in Python:
This function is True if the set is the super of the one in the brackets, that is the set has all the elements of the one in the brackets. This is similar to checking with the operator ‘>=’. If this condition is satisfied then set2 is said to be the superset of set1.
If the set2 and set2 also satisfy the condition set2>set1, then the set2 is called the proper superset of set1. For example,
Example of doing the issuperset() on sets:
set1={1,2,3,4,5} set2={1,3,4} set1.issuperset(set2) set1>=set2 set3={1,3,6} set3.issuperset(set1) set3>=set1
Output:
True
True
False
False
8. intersection_update() in Python:
This function finds the intersection of the two sets and replaces the first set with the intersection.
Example of doing the intersection_update() on sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} set1.intersection_update(set2) set1
Output:
9. difference_update() in Python:
This function finds the difference of the second set from the first and replaces the first set with the difference.
Example of doing the difference_update() on sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} set1.difference_update(set2) set1
Output:
10. symmetric_difference_update() in Python:
This function finds the non-common values from both sets and replaces the first set with the result. For example,
Example of doing the symmetric_difference_update() on sets:
set1={4,'l',5,7} set2={'PythonGeeks','6','r',7,8} set1.symmetric_difference_update(set2) set1
Output:
11. copy() in Python:
This function creates a shallow copy of a set, that is, creates the copy of the set and adds the reference to the same memory.
Example of making a copy of a set:
set1={4,'l',5,7} set2=set1.copy() set2 set1
Output:
{7, 4, 5, ‘l’}
{7, 4, 5, ‘l’}
Operations on a set in Python
We will talk about the operations that we can do on the sets.
1. Membership
There are two membership operators, namely, ‘in’ and ‘not in’. These are used to check if a value exists in the set. The function ‘in’ gives True if it exists else False. The ‘not in’ works the opposite way of ‘in’.
Example of doing membership operations on a set:
set1={4,'l',5,7} 5 in set1 'r' in set1 3 not in set1 'l' not in set1
Output:
True
False
True
False
2. Python Looping
We can go through each element of the set using the loops. The ‘for loop’ is the most common one.
Example of iteration on a set:
set1={'PythonGeeks','6','r',7,8} for i in set1: print(i)
Output:
6
PythonGeeks
7
8
Frozen Set in Python
The frozen set is the data type that has the same aspects of a set, except that these are immutable. An example of creating a frozen set is shown below.
Example of creating Python frozen set:
froz_set=frozenset([1,2,3,4]) print(f"The type of {froz_set} is {type(froz_set)}")
Output:
1. Keys in Python dictionary:
Since these are immutable, they can be used as the keys of dictionaries.
Example of creating frozen set in Python:
froz_set=frozenset([1,2,3,4]) dic=dict([[froz_set,4],['d',5]]) dic
Output:
2. Python Built-in Functions and Methods:
It supports all the functions and methods of a set which try not to modify the set. These include len(), max(), min(), sum(), all(), any(), sorted(), copy(), difference(), union(), intersection(), symmetric_difference(), isdisjoint(), issuperset(), and issubset(). If we try applying the other modifying functions, we get an error.
Example of using built-in functions on frozen set in Python:
froz_set=frozenset([1,2,3,4,5]) froz_set2=frozenset([2.4,'a',4,'p',3]) print(len(froz_set)) print(sum(froz_set)) print(max(froz_set)) f_set=froz_set.copy() f_set print(froz_set.union(froz_set2)) print(froz_set.intersection(froz_set2)) print(froz_set.difference(froz_set2)) print(froz_set.symmetric_difference(froz_set2)) froz_set3=frozenset([1,2,3]) print(froz_set.issuperset(froz_set3)) print(froz_set3.issubset(froz_set2)) print(froz_set.isdisjoint(froz_set2)) print(froz_set3.add(0))
Output:
5
15
5
frozenset({1, 2, 3, 4, 5})
frozenset({‘p’, 1, 2, 3, 4, 5, 2.4, ‘a’})
frozenset({3, 4})
frozenset({1, 2, 5})
frozenset({‘p’, 1, 2.4, 2, ‘a’, 5})
True
False
False
Traceback (most recent call last):
File “<pyshell#11>”, line 1, in <module>
print(froz_set3.add(0))
AttributeError: ‘frozenset’ object has no attribute ‘add’
Interview Questions on Sets in Python
Q1. Write a function to add a value to a set without using the add() or update functions.
Ans. We can use the union function with the second set holding the value to be added as shown below:
set1={1,2,3,4,5} set1=set1.union({6}) set1
Output:
Q2. Write a program to find the range of the values of the set.
Ans. The range is the difference between the maximum and the minimum values of the set. These can be found by using the built-in functions as shown below:
set1={3,-7,9,-4,15} MAX=max(set1) MIN=min(set1) print(MAX-MIN)
Output:
Q3. Write a program to find the number of common elements in the two sets.
Ans. We can use intersections and then find the length of the resulting set as below
set1={1,2,3,4} set2={3,5,2,8} set3=set1.intersection(set2) len(set3)
Output:
Q4. List the unique characters used in a string.
Ans. We can first convert the string into a set and then convert it into the list as shown below:
string="abcAbcBca" set1=set(string) list1=list(set1) list1
Output:
Q5. Write a program to return only the elements of the first set that are not there in the second one.
Ans. We can do the difference() operation as shown in below example:
set1={23,25,10,7} set2={25,43,17} set1.difference(set2)
Output:
Quiz on Python Sets
Conclusion
Hope you guys understood the concepts covered in this article.
To do a quick recap, first, we got introduced to the sets. We did creation, accessing and modifying the sets. Then we saw methods, functions, and operations on sets. Followed by the frozen sets. Finally, we practiced some coding questions. Happy learning!
2. Question
Which of the following is a correct way to declare an empty set?
the answer should be only B not both A and B
“>>> set_1 = {}
>>> type(set_1)
>>> “