Python | Sets data type methods

In the previous post we learnt Python Dictionaries with code examples. In this post we will learn Python Sets in detail.


Python Sets

Python set is an unordered collection with no duplicate elements. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Create Python Set

sets can be created using Curly braces {} or using set() function.

   
# sets in python

my_set_1 = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(type(my_set_1))
print(my_set_1)

my_set_2 = set(('apple', 'orange', 'apple', 'pear', 'orange', 'banana'))
print(type(my_set_2))
print(my_set_2)
 
Output
   
<class 'set'>
{'orange', 'pear', 'banana', 'apple'}
<class 'set'>
{'orange', 'pear', 'banana', 'apple'}
 

Python Sets Methods

Let's learn some of the most important methods for Python sets with code examples.

Python Sets add() method example

add() method of python sets add an element to the set. There is no effect if the element is already present.

   
# sets add() method

my_set = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(my_set)

my_set.add("grapes")
print(my_set)
 
 
Output
   
{'apple', 'pear', 'banana', 'orange'}
{'grapes', 'apple', 'banana', 'pear', 'orange'}
 

Python Sets clear() method example

clear() method of python sets removes all elements of the set.

   
# sets clear() method

my_set = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(my_set)

my_set.clear()
print(my_set)

    
 
Output
   
{'apple', 'pear', 'banana', 'orange'}
set()
 

Python Sets copy() method example

copy() method of python sets returns a shallow copy of the set.

   
# sets copy() method

my_set = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(my_set)

my_set_1 = my_set.copy()
print(my_set_1)
  
 
Output
   
{'apple', 'pear', 'orange', 'banana'}
{'apple', 'pear', 'orange', 'banana'}
 

Python Sets difference() method example

difference() method of python sets returns the difference of two or more sets as a new set. All elements in the new set are in this set but not is other sets.

   
# sets difference() method

my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
my_set_2 = {4, 5}
my_set_3 = {2, 3}
my_set_4 = {9, 10}

# difference() with one set
s_diff_1 = my_set_1.difference(my_set_2)
print(s_diff_1)

# difference() with multiple sets
s_diff_2 = my_set_1.difference(my_set_2, my_set_3, my_set_4)
print(s_diff_2)

  
 
Output
   
{1, 2, 3, 6, 7, 8, 9, 10}
{1, 6, 7, 8}

 

Python sets difference_update() method example

difference_update() method of python sets removes all elements of another set from this set.

   
# sets difference_update() method

my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
print(my_set_1)
my_set_2 = {4, 5}
print(my_set_2)

my_set_1.difference_update(my_set_2)
print("my_set_1 after applying difference_update method")
print(my_set_1)
  
 
Output
   
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{4, 5}
my_set_1 after applying difference_update method
{1, 2, 3, 6, 7, 8, 9, 10}
 

Python Sets intersection() method example

intersection() method of python sets returns intersection of two sets as a new set. New set contains elements that are in both sets.

   
# sets intersection() method

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

my_set_2 = {4, 5, 6, 9, 10, 11}
print(my_set_2)

new_set = my_set_1.intersection(my_set_2)
print(new_set)

 
Output
   
{1, 2, 3, 4, 5, 6}
{4, 5, 6, 9, 10, 11}
{4, 5, 6}
 

Python Sets intersection_update() method example

intersection_update() method of python sets updates the sets with the intersection of itself and another. i.e this set will have common elements from both sets.

   
# sets intersection_update() method

my_set_1 = {1, 2, 3, 4, 5, 6, 12}
print(my_set_1)

my_set_2 = {4, 5, 6, 9, 10, 11, 12, 13}
print(my_set_2)

print("my_set_1 after intersection_update method")
my_set_1.intersection_update(my_set_2)
print(my_set_1)
 
Output
   
{1, 2, 3, 4, 5, 6, 12}
{4, 5, 6, 9, 10, 11, 12, 13}
my_set_1 after intersection_update method
{12, 4, 5, 6}
 

Python Sets isdisjoint() method example

isdisjoint() method of python sets returns True if two sets have a null intersection (no common elements in both of the sets).

   
# sets isdisjoint() method

my_set_1 = {1, 2, 3, 4, 5, 6, 12}
my_set_2 = {4, 5, 6, 9, 10, 11, 12, 13}
my_set_3 = {20, 30, 40}
my_set_4 = {40, 50}

print(my_set_1.isdisjoint(my_set_2))
print(my_set_1.isdisjoint(my_set_3))
print(my_set_2.isdisjoint(my_set_3))
print(my_set_3.isdisjoint(my_set_4))
 
Output
   
False
True
True
False
 

Python Sets issubset() method example

issubset() method of python sets returns True if another set contains this set.

   
# sets issubset() method

my_set_1 = {1, 2, 3, 4}
my_set_2 = {1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13}
my_set_3 = {1, 2, 7, 8, 9}

print(my_set_1.issubset(my_set_2))
print(my_set_1.issubset(my_set_3))
  
 
Output
   
True
False
 

Python Sets issuperset() method example

issuperset() method of python sets returns True if this set contains another set.

   
# sets issuperset() method
my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
my_set_2 = {1, 2, 3, 4}
my_set_3 = {10, 11, 12}

print(my_set_1.issuperset(my_set_2))
print(my_set_1.issuperset(my_set_3))
  
 
Output
   
True
True
 

Python Sets pop() method example

pop() method of python sets removes and returns arbitrary set element. If set is empty is raises KeyError.

   
# sets pop() method

my_set_1 = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
print(my_set_1)

rm_element = my_set_1.pop()
print("Removed element: ", rm_element)
print("my_set_1 after pop:", my_set_1)
  
 
Output
   
{4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
Removed element:  4
my_set_1 after pop: {5, 6, 7, 8, 9, 10, 11, 12, 13}
 

Python Sets remove() method example

remove() method of python sets removes the given element from the set. If element is not found in set it raises KeyError.

   
# sets remove() method

my_set_1 = {'apple', 'pear', 'orange', 'banana'}
print(my_set_1)

# element in the set
my_set_1.remove('apple')
print(my_set_1)
my_set_1.remove('pear')
print(my_set_1)
    
 
Output
   
{'orange', 'apple', 'banana', 'pear'}
{'orange', 'banana', 'pear'}
{'orange', 'banana'}
 

Python Sets symmetric_difference() method example

symmetric_difference() method of python sets returns a new set having all elements that are in exactly one of the sets..

   
# sets symmetric_difference() method

my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
my_set_2 = {2, 8, 9, 10, 11}

new_set = my_set_1.symmetric_difference(my_set_2)
print(new_set)



# difference between `difference and symmetric_difference` methods
print("difference vs symmetric_difference")
new_set = my_set_1.difference(my_set_2)
print(new_set)

new_set = my_set_1.symmetric_difference(my_set_2)
print(new_set)
 
Output
   
{1, 3, 4, 5, 6, 7, 10, 11}
difference vs symmetric_difference
{1, 3, 4, 5, 6, 7}
{1, 3, 4, 5, 6, 7, 10, 11}
 

Python Sets symmetric_difference_update() method example

symmetric_difference_update() method of python updates a set with the symmetric difference of itself and another..

   
# sets symmetric_difference() method

my_set_1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Before applying symmetric_difference_update:", my_set_1)
my_set_2 = {2, 8, 9, 10, 11}

my_set_1.symmetric_difference_update(my_set_2)
print("After applying symmetric_difference_update:", my_set_1)
  
 
Output
   
Before applying symmetric_difference_update: {1, 2, 3, 4, 5, 6, 7, 8, 9}
After applying symmetric_difference_update: {1, 3, 4, 5, 6, 7, 10, 11}
 

Python Sets update() method example

update() method of python updates the set with the union of itself and others..

   
# sets update() method

my_set_1 = {5, 6, 7, 8, 9}
my_set_2 = {100, 200, 300}
print("my_set_1:", my_set_1)
print("my_set_2:", my_set_2)

my_set_1.update(my_set_2)
print("my_set_1 after update:", my_set_1)
  
 
Output
   
my_set_1: {5, 6, 7, 8, 9}
my_set_2: {200, 100, 300}
my_set_1 after update: {100, 5, 6, 7, 8, 9, 300, 200}
 

Python Sets union() method example

union() method of python returns a new set having all elements that are in either set..

   
# sets union() method

my_set_1 = {5, 6, 7, 8, 9}
my_set_2 = {2, 8, 9, 10, 11}
print("my_set_1:", my_set_1)
print("my_set_2:", my_set_2)

new_set = my_set_1.union(my_set_2)
print("new_set after union:", new_set)
  
 
Output
   
my_set_1: {5, 6, 7, 8, 9}
my_set_2: {2, 8, 9, 10, 11}
new_set after union: {2, 5, 6, 7, 8, 9, 10, 11}