Search

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'}

print(z)```

Example 2- Addition of existing element in the set.

```z = {'a' , 'b' , 'c'}

print(z)```

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)```

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)```

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)```

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)```

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)```

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)```

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)```

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)```

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)```

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)```

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)```

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)```

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)```

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)                                                                                                                      ```

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)```

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

Share on