L

learnasyoulike

time


Python Set Methods

Table of contents:

Python Set Methods and operations


Python set provides a large numbers of methods those support union, intersection, difference and symmetric difference of multiple sets. Some operations can be done through methods, some through operators; some operations can be executed through both. Moreover, sets being mutable, we can add or remove members, and update a set with the help of set methods.

1 2 3 4 5 6 7 8 Set A = {1, 2, 3, 4, 5} Set B = {4, 5, 6, 7, 8} Union of sets (A ∪ B) A.union(B) = A | B = {1, 2, 3, 4, 5, 6, 7, 8} Intersection of Sets (A ∩ B) A.intersection(B) = A & B = {4, 5} Difference of Sets (A \ B) A.difference(B) = A - B = {1, 2, 3} Symmetric difference of Sets (A Δ B) A.symmetric_difference(B) = A ^ B = {1, 2, 3, 6, 7, 8}

Before exploring these methods, you may have a go through the basics of python sets.

Note: All the terms 'item', 'element' and 'member' have been used synonymously. They refer to the member of a set. Here is a summarized table containing the methods of set object clicking which will take you to the respective details.


Table of contents:


MethodOperatorParamParam-typeReturns
.add()one itemNone
.remove()one itemNone
.discard()one itemNone
.pop() zero-item
.clear() zero-None
.copy() zero-set
.union()|many iterablesset
.intersection()&many iterablesset
.difference()-many iterablesset
.symmetric_difference() *^one iterableset
.isdisjoint()- oneiterableboolean
.issubset() **<=one iterableboolean
.issuperset()>=one iterableboolean
.update()|=many iterablesNone
.intersection_update()&=many iterablesNone
.difference_update()-=many iterablesNone
.symmetric_difference_update()^= oneiterableNone

* Though the symmetric_difference() method accepts only one iterable, the equivalent operator ^ can operate on multiple sets.

** To check whether a set is a proper subset of another set, we can use the < operator.

** To check whether a set is a proper superset of another set, the > operator can be used.
There are no methods for the above two operations.

Important: When we use an operator instead of a method, we must provide sets, not iterables.


In the following, the set methods are grouped as per their behaviours. Click the button to see/hide them.


Show grouped methods

Set methods

Method set.add() Add an item to set

The set.add(item) method adds a single immutable item to the specified set.


We can add a tuple and strings in this way, because they are immutable.

But we cannot add mutable objects like list, set or dictionary in this way.

Method set.remove() - Remove an item from set

The set.remove(item) method removes an item from a set.

If the given item is not found in the set, a KeyError is raised.

Method set.discard() - Remove an item from set

The set.discard(item) method also removes an item from a set.

If the given item is not found in the set, no error is raised.

Method set.pop() - Remove an item from set

The set.pop() method removes a random item from a set. The removed item is returned as output.

The item to be removed is selected randomly; in the above case, 'firefox' was the target. If the given the set is empty, python raises a KeyError.

Method set.clear() - Remove all items of a set

The set.clear() method removes all items from a set.

Method set.copy() - Copy a set

The set.copy() method returns a copy of the given set.

The identity of the new variable will be different than the old one.





Operations on multiple sets

Method set.union() - Union of sets

The union() method returns a new set containing all the items of the given set and given iterable(s). Obviously, duplicate items, if any, will be dropped.

This method can receive multiple itarables in the following format:

set1.union(iterable1, iterable2 ...)

where set1 is the python set to operate on; iterable1 may be any python iterables like a list, tuple, set, dictionary or string. If the iterable is not a set, then it will be converted to set first.

Internally, the method will take the shape as follows:

set1.union(set2, set3 ...)

At least one iterable is mandatory as a parameter for this method.

The iterable(list) [2, 3, 4] is converted to a set before union.


The iterable(tuple) ("a", "d", "f") is converted to a set before union.


In the following, a set is united with another set.



If the iterable is a dictionary, it is converted to a set.


The iterable string is converted to a set.



The .union() method can operate on multiple iterables.

In the above, the given iterables have been converted to sets and united with the set {1, 2}.


Operator | is equivalent to .union() method

The operator | can also be used instead of the .union() method. The syntax takes the format:

set1 | set2 [| set3...]

The above notation translates as:



Using multiple sets with .union() method and the | operator:


Unlike the .union() method, the | operator does not allow any iterable, but only sets. The expression {1, 2} | [3, 4] will raise a TypeError.

{1, 2}.union( [3, 4] )# {1, 2, 3, 4}
{1, 2} | {3, 4}  # {1, 2, 3, 4}
{1, 2} | [3, 4]  # TypeError

Method set.update() - Union of sets

The .update(iterables) method updates the original set with all the items of the original set and the given iterable(s). It is similar to the .union() method, except that it does not return any new set, but modifies the original set.


set1 (original set) is updated with the union of {0, 1} and {10, 11}.


The |= operator is equivalent to .update() method

The operator |= can also be used instead of the .update() method. We use the above example with the |= operator:

We get the same result as with .update() method.

The |= is known as augmented assignment operator which we have already dealt with.

The .update() method accepts iterables, but |= operator accepts sets only.

Method set.intersection() - Intersection of sets

The .intersection(iterables) method returns a new set containing items which are common to the given set and iterable(s). If the iterable is not a set, it is converted to a set.

Items 2.4 and 5 are common to both the given sets, so {2.4, 5} is the output.


Item 'mass' is common to both set1 and set2.


Operator & is equivalent to .intersection() method

The operator & can also be used instead of the .intersection() method. The syntax is:

set1 & set2 [& set3...]


The .intersection() method can operate on iterables, but the & operator requires its operands to be sets.

{1, 2}.intersection( (2, 3, 4) )# {2}
{1, 2} & {2, 3, 4}  # {2}
{1, 2} & (2, 3, 4)  # TypeError


When no common element is found, the .intersection() method returns an empty set.

{1, 2}.intersection( [3, 4] )# set()
{"a", "b"} & {"A", "B"}     # set()

Method set.intersection_update() - Intersection of sets

The intersection_update(iterables) method updates the original set with all the common items of the original set and the given iterable(s). It is similar to the .intersection() method, except that it does not return any new set, but modifies the original set.


set1 (original set) is updated and contains the common items of {100, 101} and {101, 110, 111}.


Operator &= is equivalent to .intersection_update() method

The operator &= can also be used instead of the .intersection_update() method. We use the above example with the &= operator:


set1 now contains only the common item 101.
The .intersection_update() method accepts iterables, but &= operator accepts sets only.

Method set.difference() - Difference of sets

The .difference(iterables) method returns a new set that includes the items of first set those are not present in the second set. Any iterable, if not a set, is converted to a set.

Items "c" and "d" are dropped in the output set, because they are present in the second set.


Operator - is equivalent to .difference() method

The - operator can be used instead of the .difference() method. The syntax is:

set1 - set2 [ - set3...]

In case of more than two sets, as in the following,
set1 - set2 - set3 - set4
the operation proceeds from left to right.


Like .union(), the .difference() method can operate on iterables, but the operator - requires its operands to be sets.

Method set.difference_update() - Difference of sets

The difference_update(iterables) method updates the original set with the difference of the original set and the given iterable(s). It is similar to the .difference() method, except that it does not return any new set, but modifies the original set.


set1 (original set) is updated and contains the result of
{1000, 1001, 1010} - {1010, 1011, 1100}.


Operator -= is equivalent to .difference_update() method

The operator -= can also be used instead of the .difference_update() method. We use the above example with the -= operator:


Using the operator -= we get the same result as the .difference_update() method.
The method accepts iterables, but the -= operator accepts sets only.

Method set.symmetric_difference() - Symmetric difference of sets

The .symmetric_difference(iterable) method returns a new set that includes all the members of the given sets except the common members.

This method receives a single iterable as its parameter.

Since members 6 and 8 are common to both the sets, they are excluded in the output set.


Operator ^ is equivalent to .symmetric_difference() method

The ^ operator can be used instead of the .symmetric_difference() method. The syntax is:

set1 ^ set2 [ ^ set3...]


In case of more than two sets, as in the following,
set1 ^ set2 ^ set3 ^ set4
the operation proceeds from left to right.

Unlike previous methods, the .symmetric_difference() method accepts a single iterable. But surprisingly, the operator ^ can operate on multiple sets, as we have seen in the above example.

Method set.symmetric_difference_update() - Symmetric difference of sets

The symmetric_difference_update(iterable) method updates the original set with all the members of the given sets except the common members. It is similar to the .symmetric_difference() method, except that it does not return any new set, but modifies the original set.


The common member 1110 is dropped, and all the other members are contained in set1.


Operator ^= is equivalent to .symmetric_difference_update() method

The operator ^= can also be used instead of the .symmetric_difference_update() method. We use the above example with the ^= operator:


Using the operator ^= we get the same result as with .difference_update() method.
This method accepts one iterable, but -= operator accepts sets only.

Method set.isdisjoint() - Check if two sets have common members

The .isdisjoint(iterable) method returns a boolean value True if the specified set and the iterable do not have any common members. If they have common member(s), a False is returned.

The isdisjoint() method operates on a single iterable. There is no alternative operator for this method.

Since there is no common member between {11, 22, 33} and {44, 55}, the first output is True.

But there is a common member 11 between {11, 22, 33} and {66, 11, 77}, so the second output is False.

What are subsets?

If every item of a set s1 is in another set s2, then s1 is a subset of set s2.


In other words, if s1 <= s2, then s1 is a subset of set s2.


Note: An empty set set(), also known as a null or void set, is a subset of any set.



What are proper subsets?

If every item of a set s1 is in another set s2, and s1 is not equal to s2, then s1 is a proper subset of set s2.


In other words, if s1 < s2, then s1 is a proper subset of set s2.


Method set.issubset() - Check if one set is subset of another set

The .issubset(iterable) method returns True if the original set is a subset of the given set, else a False is returned.

This method receives a single iterable as its parameter.


Operator <= is equivalent to .issubset() method

Instead of the method .issubset(), the <= operator can also be used to check whether a set is a subset of another set or not.



Check if proper subset

The < operator for proper subset

To check whether a set is a proper subset of another set or not, there is no method, but we can use the < operator to do this.

Note that {1, 2} cannot be a proper subset of {1, 2}, because they are equal. But {1}, {2} and set() are proper subsets of {1, 2}.

What are supersets?

If a set s1 contains every item of another set s2, then s1 is a superset of set s2.


In other words, if s1 >= s2, then s1 is a superset of set s2.


Note: Any set is a superset of the empty set set(), which is also known as a null or void set.



What are proper supersets?

If a set s1 contains every item of another set s2, and s1 is not equal to s2, then s1 is a proper superset of set s2.


In other words, if s1 > s2, then s1 is a proper superset of set s2.


Method set.issuperset() - Check if one set is superset of another set

The .issuperset(iterable) method returns True if the original set is a superset of the given set, else a False is returned.

This method receives a single iterable as its parameter.


Operator >= is equivalent to .issuperset() method

Instead of the method .issuperset(), the >= operator can be used to check whether a set is a superset of another set or not.



Operator > for proper superset

To check whether a set is a proper superset of another set or not, there is no method, but we can use the > operator to do this.

Note that {1, 2} cannot be a proper superset of {1, 2}, because they are equal. But {1, 2} is a proper superset of {1}, {2} and set().



It is easier to use operators instead of methods to check whether a set is a subset or proper subset or superset or proper superset of an another set.






--- x ---









Want to leave a message for me?