Skip to content

Python Set




Python – Set


As we earlier about Python Dictionary , Now we will study about Sets in python. In the python programming language , this is our fourth collection of data type. In this, we will cover set in python, how to create a set in python, python set example, set methods python, set operations python etc. so let’s start study about the fourth collection data type.





Set in python

Set is a collection of heterogeneous data type. It is an unordered collection of elements. It is a mutable data type i.e. they can be edited or modified. Set can have only unique values.

In python programming, sets are written with Curly Brackets.



Python set syntax :

set variable = { comma separated element }



Python set example : How to create a set in python ?

set_1 = {"Black", 5, "White", 9, "Red"}
print(set_1)



Output :

{5, 'White', 9, 'Red', 'Black'}

NOTE : Each time the output will be different because the sets are unordered collection of elements.





How to check the type of set ?

In the above example, we can check the type of set_1. 



Example :

set_1 = {"Black", 5, "White", 9, "Red"}
print(type(set_1))



Output:

<class 'set'>




How to access the set items from a set ?

In set, we cannot access the elements of set by using the index number because sets are unordered, the set items has no index position.

But we can access the elements of set by using the “for” loop.



Example :

set_1 = {"Black", 5, "White", 9, "Red"}
for e in set_1:
    print(e)



Output :

Black
5
Red
9
White




To check the value is present or not in a set.

We can also check the set values is present or not present in a set by using in” keyword.



Example :

set_1 = {"Black", 5, "White", 9, "Red"}
print("White" in set_1)



Output :

True




Another Example : Not present

set_1 = {"Black", 5, "White", 9, "Red"}
print("Blue" in set_1)



Output :

False




How to change the item/values of a set ?

Once a set is created, we cannot change the items / values of a set.





How to add items in a set ?

We can add items into set by using the python add( ) method.



Syntax for add items :

set_variable.add( element )



Example :

set_1 = {"Black", 5, "White", 9, "Red"}
set_1.add("Blue")
print(set_1)



Output :

{'White', 5, 'Red', 9, 'Black', 'Blue'}




How to Remove the items ?

We can remove the items by using the python remove( ) method.



Syntax to remove items :

set_variable.remove( elements )



Example :

set_1 = {"Black", 5, "White", 9, "Red"}
set_1.remove("Red")
print(set_1)



Output :

{5, 'Black', 9, 'White'}



It is an alternative method to remove the items by using discard( ) method.



Syntax to remove items :

set_variable.discard( elements )



Example :

set_1 = {"Black", 5, "White", 9, "Red"}
set_1.discard("Red")
print(set_1)



Output :

{'Black', 5, 9, 'White'}




How to check the length of a given set ?

To check how many items a set has, by using the len( )  function.



Example :

set_1 = {"Black", 5, "White", 9, "Red"}
print(len(set_1))



Output :

5




Another Example :

set_1 = {"Black", 5, "White", 8, "Black", 5}
print(len(set_1))



Output :

4

NOTE : In the above example, the output is 4 because sets can have only unique values.





Joining two sets

To join or concatenate two or more sets in python by using the union( ) method.



Example :

set_1 = {1, 2, 3}
set_2 = {4, 5, 6}
set_3 = set_1.union(set_2)
print(set_3)



Output :

{1, 2, 3, 4, 5, 6}

The union( )  method always returns a new set containing all items from both set.




It is an alternative method to join two or more sets by using the update( ) function.



Example :

set_1 = {1, 2, 3}
set_2 = {4, 5, 6}
set_1.update(set_2)
print(set_1)



Output :

{1, 2, 3, 4, 5, 6}




Python set( ) constructor

This is an alternative to make a new set by using the set( ) constructor.

It is also a solution to make an empty set by using the set( ) constructor.



Example :

set_1 = set(("Black", 5, "White", 9, "Red"))
print(set_1)



Output :

{5, 'Black', 9, 'White', 'Red'}




Python Set Methods :



1. difference( ) : 

It returns a set containing the difference between two or more sets.



Python difference( ) syntax :

set_1.difference(set_2)    # for 1 - 2
set_2.difference(set_1)    # for 2 - 1



Python difference( ) Example :

set_1 = {"Black", 5, "White", 8, "Blue", 5}
set_2 = {2, "Black", 6, "Red", 5}
set_3 = set_1.difference(set_2)
print(set_3)



Output :

{8, 'Blue', 'White'}

1 – 2 is equal to the elements present in 1 but not in 2.
2 – 1 is equal to the elements present in 2 but not in 1.





2. difference_update( ) :

It removes the items in this set that are also included in another.



Python difference_update ( ) syntax :

set_1.difference_update(set_2)
set_2.difference_update(set_1)



Python difference_update( ) Example :

set_1 = {"Black", "White", "Red"}
set_2 = {"Yellow", "Pink", "Black"}
set_1.difference_update(set_2)
print(set_1)



Output :

{'White', 'Red'}




3. intersection( ) : 

It returns a set, i.e the intersection of two other sets.



Python intersection( ) syntax :

set_1.intersection(set_2)      # for set_1
set_2.intersection(set_1)      # for set_2



Python intersection( ) example :

set_1 = {"Black", "White", "Red", "Pink"}
set_2 = {"Yellow", "Pink", "Black"}
set_3 = set_1.intersection(set_2)
print(set_3)



Output :

{'Pink', 'Black'}




4. intersection_update( ) : 

It removes the items in set_1 that are not present in set_2.



Python intersection_update( ) syntax :

set_1.intersection_update(set_2)     # for set_1
set_2.intersection_update(set_1)     # for set_2



Python intersection_update( ) example :

set_1 = {"Black", "White", "Red", "Pink"}
set_2 = {"Yellow", "Pink", "Black"}
set_1.intersection_update(set_2)
print(set_1)



Output :

{'Pink', 'Black'}




5. isdisjoint( ) : 

It returns whether two sets have a intersection or not.



Python isdisjoint( ) syntax :

set_1.isdisjoint(set_2)



Python isdisjoint( ) example :

set_1 = {"Black", "White", "Red", "Pink"}
set_2 = {"Yellow", "Orange", "Blue"}
set_3 = set_1.isdisjoint(set_2)
print(set_3)



Output :

True

It returns True if none of the items are present in both sets, otherwise returns False if items are not present in both sets.





6. issubset( ) : 

It returns whether another sets contains this set or not.



Python issubset( ) syntax :

set_1.issubset(set_2)     # for set_1
set_2.issubset(set_1)     # for set_2



Python issubset( ) example :

set_1 = {"Black", "White", "Red"}
set_2 = {"Yellow", "Black", "White", "Red" }
set_3 = set_1.issubset(set_2)
print(set_3)



Output :

True

It returns True if all the items are present in the specified set, otherwise returns False.





7. issuperset( ) : 

It returns whether this set contains another set or not.



Python issuperset( ) syntax :

set_1.issuperset(set_2)



Python issuperset( ) example :

set_1 = {"Black", "White", "Red"}
set_2 = {"Yellow", "Black", "White", "Red" }
set_3 = set_1.issuperset(set_2)
print(set_3)



Output :

False

It returns True if all the items in this specified set exists in the original set, otherwise return False





8. symmetric_difference( ) : 

It returns a set with the symmetric difference of two sets.



Python symmetric_difference( ) syntax :

set_1.symmetric_difference(set_2)



Python symmetric_difference( ) example :

set_1 = {"Black", "White", "Red"}
set_2 = {"Yellow", "Black", "White", "Red" }
set_3 = set_1.symmetric_difference(set_2)
print(set_3)



Output :

{'Yellow'}

It returns a set contains a mix of items that are not present in both the sets.




9. symmetric_difference_update( ) :

It remove the items that are present in both the sets, and insert the items that is not present in both the sets.



Python symmetric_difference_update ( ) syntax :

set_1.symmetric_difference_update(set_2)



Python symmetric_difference_update ( ) example :

set_1 = {"Black", "White", "Red"}
set_2 = {"Yellow", "Blue", "Black"}
set_1.symmetric_difference_update(set_2)
print(set_1)



Output :

{'Yellow', 'Red', 'White', 'Blue'}



>