Introduction to Python Set Methods with Practical Examples

In This Article You Will Learn About Python Set Methods

Python Set Methods – Before moving ahead, let’s know about Introduction to Python Set

Set Methods – Python has a set of built-in methods that can be used with set.

Some of them we have already been discussed rest of we are going to discuss now.

add() method – It used to add an element in set. If element is already present in set, then it doesn’t add anything.

Syntax - set.add(element)

Parameter Values -

element - It is required argument. It takes element which is to be added.

Example 1- Addition of element in the set.

z = {'a' , 'b' , 'c'}
z.add('d')

print(z)
python-set-methods
As it is shown clearly that element ‘d’ is added to set and returned a new set with adding element. 

Example 2- Addition of existing element in the set.

z = {'a' , 'b' , 'c'}
z.add('b')

print(z)
python-set-methods
As it is shown clearly that it returned set with containing element ‘b’ only one time.  

 copy() method – It used to copy the set.

Syntax - set.copy()

Parameter Values - No parameter values.

Example – Use copy() method to copy the set.

z = {'a' , 'b' , 'c'}
x = z.copy()

print(x)
python-set-methods
As it is shown clearly that it returned the copy of set.

difference() method – It returns a set which contains difference of two sets. In other-words, it returns only that elements which are present only in first set not in both sets.

Syntax - set. difference(set)

Parameter Values - The set to be checked for difference.

Example 1- It returns a set of elements that is present only in set z, and not in set x.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'a'}
old_set_difference = old_set.difference(new_set)

print("old updated set is:" , old_set_difference)
python-set-methods
As it is shown clearly that it returned updated old _set with containing only elements of old_set not present in new_set.

Example 2- It returns a set of elements that is present only in set new_set, and not in old_set.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'a'}
old_set_difference = new_set.difference(old_set) 

print("new updated set is:" , old_set_difference)
python-set-methods
As it is shown clearly that it returned updated new_set with containing only elements of new_set not present in old_set.

difference_update() method – It removes element that is present in both sets.

Syntax - set .difference_update(set)

Parameter Values -

set - It is required argument. The set to be checked for difference.

Example – It returns a set of elements presents only in old_set between the comparison of old_set with new_set

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'k'}
old_set.difference_update(new_set) 

print("new updated set is:" , old_set)
python-set-methods
As it is shown clearly that it returned an old updated set of containing only different elements of old_set.

intersection() method – It returns set of similar elements that is present in two or more set.

Syntax - set .intersection( set1, set2 ... etc.)

Parameter Values -

set1 - It is required argument. The set to search for equal elements in.

set2 - It is optional argument. The other set to search for equal elements in.

Example 1- It returns set of similar elements that is present in both sets.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'k'}
old_updated_set = old_set.intersection(new_set)

print("old updated set is:" , old_updated_set)
python-set-methods
As it is shown clearly that it returned a set containing similar elements from both sets.

Example 2- It returns set of similar elements that is present in all sets between the comparison of old_set with other sets.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'k'}
extra_set = {'b' , 'd' , 'l'}
old_updated_set = old_set.intersection(new_set, extra_set)

print("old updated set is:" , old_updated_set)
python-set-methods
As it is shown clearly that it returned a set containing similar elements from all sets.

intersection_update() method – It removes the elements that is not present in two or more sets.                                                                                                                                                                                                                                      

Syntax - set.intersection_update(set1, set2, ... etc )

Parameter Values -

set1 - It is required argument. The set to search for equal elements in.

set2 - It is optional argument. The other set to search for equal elements in.

Example 1- It removes elements that is not present in both sets.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'k'}
old_set.intersection_update(new_set)

print("old updated set is:" , old_set)
python-set-methods

Example 2- It returns set of similar elements that is present in all sets between the comparison of new_set with other sets.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'k'}
extra_set = {'b' , 'd',  'l'}
new_set.intersection(old_set, extra_set)

print("new updated set is:" , new_updated_set)
python-set-methods
As it is shown clearly that it returned only common element in all sets.

isdisjoint() method – It returns True if all elements are not present in both sets, otherwise False.

Syntax - set.isdisjoint(set)

Parameter Values -

set - It is required argument. It is the set to be compared with.

Example 1- It returns True if none of elements common in both sets.

old_set = {'a' , 'b' , 'c'}
new_set = {'h' , 'g' , 'k'}
old_updated_set = old_set.isdisjoint(new_set) 

print(old_updated_set)
python-set-methods
As it is shown clearly that all elements are different in both sets therefore, it returned False.

issubset() method – It returns True, if all elements of set one contains in the another set, otherwise False.

Syntax - set.issubset(set)

Parameter Values -

set - It is required argument. It is set to be compared with.

Example 1- It returns True, if all elements of set one present in another set.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'c' , 'g'}
old_updated_set = old_set.issubset(new_set)

print(old_updated_set)
python-set-methods
As it is shown clearly that all elements of old_set is same in new_set therefore, it returned True.

Example 2- It returns False, if all elements of set one is not present in another set.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'c' , 'k'}
old_updated_set = old_set.issubset(new_set)  

print(old_updated_set)
python-set-methods
As it is shown clearly that all elements of old_set is not same in new_set therefore, it returned False.

issuperset() method – It returns True, if all elements present in the original set, otherwise False.

Syntax - set.issuperset(set)

Parameter Values -

set - It is required argument. It is set to be compared with.

Example 1- It returns True, if all elements of original set is present in other set.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'a' , 'c'}
old_updated_set = old_set.issuperset(new_set)

print(old_updated_set)
python-set-methods
As it is shown clearly that it returned True, as all elements of both sets is same.

Example 2- It returns False, if all elements of original set is not present in other set.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'c'}
old_updated_set = old_set.issuperset(new_set)

print(old_updated_set)
python-set-methods
As it is shown clearly that it returned False, as all elements of both sets are not same.

symmetric_difference() method – It returns all elements of set except common elements present in both sets.

Syntax - set.symmetric_difference(set)

Parameter Values -

set - It is set to be compared with.

Example – It returns set that contains all elements from both set except common elements.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'k'}
old_updated_set = old_set.symmetric_difference(new_set)

print(old_updated_set)                                                                                                                      
python-set-methods
As it shown clearly that it returns a set that contains only those elements which are not same in both sets.

symmetric_difference_update() method – It removes all common elements present in both sets, and update original set after inserting elements of another set.

Example – It removes all common elements present in both sets, and insert elements which are not present in original set.

old_set = {'a' , 'b' , 'c'}
new_set = {'b' , 'g' , 'k'}
old_set.symmetric_difference_update(new_set) 

print(old_set)
python-set-methods
As it shown clearly that it returns a set that contains only those elements which are not same in both sets.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Leave a Comment

Stay in the loop

codingstreets