Python set and its methods with examples.

Python set is an iterable collection and its not different than a mathematical set of objects. The following are the different properties of this.

  1. A set does not contain duplicate values.
  2. Order of the elements not necessarily be same as its order of insertion.
  3. Items can not be modifiable, whereas we can modify set as a whole.

Python sets support many methods which are helpful to do all mathematical operations. Below is the list of ways in it.

  1. add
  2. copy
  3. clear
  4. difference
  5. difference_update
  6. discard
  7. intersection
  8. intersection_update
  9. isdisjoint
  10. issubset
  11. issuperset
  12. pop
  13. remove
  14. symmetric_difference
  15. symmetric_difference_update
  16. union
  17. update

add

This method adds the element to the set. It requires an object parameter and returns none. Let’s see an example of this function.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
print(set1)
set1.add(55)
print(set1)
# prints
{5, 10, 15, 20, 25, 30}
{5, 10, 15, 20, 55, 25, 30}

copy

This method copies the set into another set. Copy method doesn’t require any parameter and returns the copy of the set object. See the below example of the copy method.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
print(set2)
# prints
{20, 5, 55, 25, 10, 30, 15}

clear

This method clears all objects in the set. This function also doesn’t require any parameter and returns none. Let’s see an example of this function.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set1.clear()
print(set1)
# prints empty set
set()

difference ( – )

This method gives the difference between the current set, with the iterator in the parameter. It requires an iterable parameter and returns a set object. The example code is as follows.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
print(set4)
# prints
{20, 55, 25, 10, 30, 15}
# removes the common element 5 from set2

difference_update

Unlike the set method difference, the difference_update method gets the difference and updates in the current set itself. This method requires an iterable object and returns none. The example code is as follows.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set1.clear()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
set2.difference_update(set3)
print(set2)
# prints
{20, 55, 25, 10, 30, 15}
# updates the set2 by removing the common element.

discard

This function discards the item in the parameter from the set object. This function requires an object parameter that we want to remove, and this function returns none. The example code is as follows.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set2.difference_update(set3)
set2.discard(10)
print(set2)
# prints
{20, 55, 25, 30, 15}
# 10 is not in the set, as it discarded from discard() function.

intersection ( & )

This function gives the intersection of the current settings object with the object in the parameter. It requires an iterable object as a parameter and returns the intersection set. Let’s see an example code.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
set2.difference_update(set3)
set2.discard(10)
set5 = set2.intersection(set4)
print(set5)
# prints
{15, 20, 55, 25, 30}

intersection_update

Unlike the intersection method, the intersection_update method updates the intersection set in the current object itself. It requires one iterable parameter and updates the result into the current object itself. Let’s see the code for this method.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
set2.difference_update(set3)
set2.discard(10)
set5 = set2.intersection(set4)
set2.intersection_update(set4)
print(set2)
# prints
{15, 20, 55, 25, 30}

isdisjoint

This method checks whether the current set is disjoint set to the object in the parameter one. This method requires one parameter which is set and returns either True or False. Let’s see the code for this function.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
set2.difference_update(set3)
set2.discard(10)
set5 = set2.intersection(set4)
set2.intersection_update(set4)
print(set2.isdisjoint(set3))
# prints
True
# set2 an set3 are disjoints so it returns True.

issubset ( <= )

Is subset checks whether the current set is a subset with the collection in the parameter or not? It requires a parameter set and returns either True or False. The example code is as shown below.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
set2.difference_update(set3)
set2.discard(10)
set5 = set2.intersection(set4)
set2.intersection_update(set4)
print(set2.issubset(set3))
# prints
False
# set2 is not subset of set3, so printing False.

issuperset ( >= )

This method checks whether the current set is a superset to the collection in the parameter. This method requires one iterable, which is a set with which we are comparing with, and this returns either True or False. Let us see some code of this method.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
set2.difference_update(set3)
set2.discard(10)
set5 = set2.intersection(set4)
set2.intersection_update(set4)
print(set4.issuperset(set2))
# prints
True
# set4 is superset to set2, so returning True.

pop

This method pops an item from the set and returns it to the caller. This function requires no parameters and returns the popped object. The sample code is as follows.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
set2.difference_update(set3)
set2.discard(10)
set5 = set2.intersection(set4)
set2.intersection_update(set4)
print(set2.pop())
print(set2)
# prints
15
{20, 55, 25, 30}

remove

This method removes the object in the parameter from the set. It requires a setting that we want to withdraw from the current collection and returns none. The sample code is as follows.

list1 = [5, 10, 15, 20, 25, 30]
set1 = set(list1)
set1.add(55)
set2 = set1.copy()
set3 = {2, 5, 8}
set4 = set2.difference(set3)
set2.difference_update(set3)
set2.discard(10)
set5 = set2.intersection(set4)
set2.intersection_update(set4)
set2.pop()
set2.remove(55)
print(set2)
# prints
{20, 25, 30}
# removed 55 from the set and prints.

symmetric_difference ( ^ )

As in the difference function, in which elements in the first set which are not in the second set (which is in parameter), here in symmetric_difference the items in both the collections which are not present in other, it requires on iterable parameter and returns the resultant set, let’s see this with an example.

a = {1, 2, 3}
b = {3, 4, 5}

c = a.symmetric_difference(b)
print(c)
# prints
{1, 2, 4, 5}
# here the resultant set contains all items except common items.

symmetric_difference_update

Unlike the symmetric_difference method, it updates the current set itself with all items in both the sets except common items. This also requires an iterable parameter and returns none. Let’s see the code for this method.

a = {1, 2, 3}
b = {3, 4, 5}

a.symmetric_difference_update(b)
print(a)
# prints
{1, 2, 4, 5}

union ( | )

This method gives the union of the current set and the set in the parameter. This method requires an iterable parameter and returns the union set. The example code is as follows.

a = {1, 2, 3}
b = {3, 4, 5}

c = a.union(b)
print(c)
# prints
{1, 2, 3, 4, 5}

update

The update method updates the current set with the elements in the iterable parameter elements. This method requires an iterable parameter and returns none. Let’s see the below example code.

a = {1, 2, 3}
a.update({2, 4, 8})
print(a)
# prints
{1, 2, 3, 4, 8}
# similar to union it updates with parameter contents.

These are all the different methods supported in the python set. We hope this helps you understand all different functions in set, happy learning!.

Advertisements