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.
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.
Method | Operator | Param | Param-type | Returns |
---|---|---|---|---|
.add() | one | item | None | |
.remove() | one | item | None | |
.discard() | one | item | None | |
.pop() | zero | - | item | |
.clear() | zero | - | None | |
.copy() | zero | - | set | |
.union() | | | many | iterables | set |
.intersection() | & | many | iterables | set |
.difference() | - | many | iterables | set |
.symmetric_difference() * | ^ | one | iterable | set |
.isdisjoint() | - | one | iterable | boolean |
.issubset() ** | <= | one | iterable | boolean |
.issuperset() | >= | one | iterable | boolean |
.update() | |= | many | iterables | None |
.intersection_update() | &= | many | iterables | None |
.difference_update() | -= | many | iterables | None |
.symmetric_difference_update() | ^= | one | iterable | None |
*
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.
In the following, the set methods have been grouped according to their behaviours. This has been done only for better understanding.
Methods those accept multiple iterables as parameters
Methods those accept a single iterable as parameter
Methods those accept a single immutable item as parameter
Methods those accept no parameter
Methods those return a value (not None)
None of the above methods modifies the original set, except the method .pop()
Methods those have an equivalent operator
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.
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.
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.
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.
The set.clear() method removes all items from 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.
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}.
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
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 |= 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.
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.
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()
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}.
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.
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.
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.
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}.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Instead of the method .issubset(), the <= operator can also be used to check whether a set is a subset of another set or not.
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}.
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.
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.
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.
Instead of the method .issuperset(), the >= operator can be used to check whether a set is a superset of another set or not.
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?