Python - Set

A set is a mutable collection of distinct hashable objects, same as the list and tuple. It is an unordered collection of objects, meaning it does not record element position or order of insertion and so cannot access elements using indexes.

The set is a Python implementation of the set in Mathematics. A set object has suitable methods to perform mathematical set operations like union, intersection, difference, etc.

A set object contains one or more items, not necessarily of the same type, which are separated by a comma and enclosed in curly brackets {}. The following defines a set object with even numbers.

Example: Python Set Object
even_nums = {2, 4, 6, 8, 10} # set of even numbers
emp = {1, 'Steve', 10.5, True} # set of different objects

A set doesn't store duplicate objects. Even if an object is added more than once inside the curly brackets, only one copy is held in the set object. Hence, indexing and slicing operations cannot be done on a set object.

Example: Set of Distinct Elements
>>> nums = {1, 2, 2, 3, 4, 4, 5, 5}
>>> nums
{1, 2, 3, 4, 5}

The order of elements in the set is not necessarily the same as the order given at the time of assignment. Python optimizes the structure of a set for performing operations over it, as defined in mathematics.

Only immutable (and hashable) objects can be a part of a set object. Numbers (integer, float, as well as complex), strings, and tuple objects are accepted, but set, list, and dictionary objects are not.

Example: Set Elements
>>> myset = {(10,10), 10, 20} # valid
>>> myset
{10, 20, (10, 10)}
>>> myset = {[10, 10], 10, 20}  # can't add a list
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    myset = {[10, 10], 10, 20}
TypeError: unhashable type: 'list'
>>> myset = { {10, 10}, 10, 20} # can't add a set
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    myset = { {10, 10}, 10, 20}
TypeError: unhashable type: 'set'

In the above example, (10,10) is a tuple, hence it becomes part of the set. However, [10,10] is a list, hence an error message is displayed saying that the list is unhashable. (Hashing is a mechanism in computer science which enables quicker search of objects in the computer's memory.)

Even though mutable objects are not stored in a set, the set itself is a mutable object.

Use the set() function to create an empty set. Empty curly braces will create an empty dictionary instead of an empty set.

Example: Creating an Empty Set
>>> emp = {} # creates an empty dictionary
>>> type(emp)
<class 'dict'>
>>> s = set() # creates an empty set
>>> type(s)
<class 'set'>

The set() function also use to convert string, tuple, or dictionary object to a set object, as shown below.

Example: Convert Sequence to Set
>>> s = set('Hello') # converts string to set
>>> s
{'l', 'H', 'o', 'e'}
>>> s = set((1,2,3,4,5)) # converts tuple to set
>>> s
{1, 2, 3, 4, 5}
>>> d = {1:'One', 2: 'Two'} 
>>> s = set(d) # converts dict to set
>>> s
{1, 2}

Modify Set Elements

Use built-in set functions add(), remove() or update() methods to modify set collection.

Example:
>>> s = set() # creates an empty set
>>> s.add(10) # add an element
>>> s.add(20)
>>> s.add(30)
>>> s
{10, 20, 30}
>>> primeNums = {2, 3, 5, 7}
>>> s.update(primeNums) # update set with another set
>>> s
{2, 3, 20, 5, 7, 10, 30}
>>> s.remove(2) # remove an element
>>> s
{3, 20, 5, 7, 10, 30}
ADVERTISEMENT

Set Operations

As mentioned earlier, the set data type in Python implements as the set defined in mathematics. Various set operations can be performed. Operators |, &, - and ^ perform union, intersection, difference, and symmetric difference operations, respectively. Each of these operators has a corresponding method associated with the built-in set class.

Operation Example
Union: Returns a new set with elements from both the sets.

Operator: |
Method: set.union()
>>> s1={1,2,3,4,5}
>>> s2={4,5,6,7,8}
>>> s1|s2         
{1, 2, 3, 4, 5, 6, 7, 8}
>>> s1={1,2,3,4,5}
>>> s2={4,5,6,7,8}
>>> s1.union(s2)  
{1, 2, 3, 4, 5, 6, 7, 8}   
>>> s2.union(s1)  
{1, 2, 3, 4, 5, 6, 7, 8}
Intersection: Returns a new set containing elements common to both sets.

Operator: &
Method: set.intersection()
>>> s1={1,2,3,4,5}
>>> s2={4,5,6,7,8}
>>> s1&s2         
{4, 5}                     
>>> s2&s1         
{4, 5}
>>> s1={1,2,3,4,5}         
>>> s2={4,5,6,7,8}         
>>> s1.intersection(s2)    
{4, 5}                              
>>> s2.intersection(s1)    
{4, 5}
Difference: Returns a set containing elements only in the first set, but not in the second set.

Operator: -
Method: set.difference()
>>> s1={1,2,3,4,5}
>>> s2={4,5,6,7,8}
>>> s1-s2         
{1, 2, 3}                  
>>> s2-s1         
{8, 6, 7}
>>> s1={1,2,3,4,5}    
>>> s2={4,5,6,7,8}    
>>> s1.difference(s2) 
{1, 2, 3}                      
>>> s2.difference(s1) 
{8, 6, 7}
Symmetric Difference: Returns a set consisting of elements in both sets, excluding the common elements.

Operator: ^
Method: set.symmetric_difference()
>>> s1={1,2,3,4,5}
>>> s2={4,5,6,7,8}
>>> s1^s2         
{1, 2, 3, 6, 7, 8}         
>>> s2^s1         
{1, 2, 3, 6, 7, 8}
>>> s1={1,2,3,4,5}             
>>> s2={4,5,6,7,8}             
>>> s1.symmetric_difference(s2)
{1, 2, 3, 6, 7, 8}                      
>>> s2.symmetric_difference(s1)
{1, 2, 3, 6, 7, 8}

Set Methods

The following table lists built-in set methods:

Method Description
set.add() Adds an element to the set. If an element is already exist in the set, then it does not add that element.
set.clear() Removes all the elements from the set.
set.copy() Returns a shallow copy of the set.
set.difference() Returns the new set with the unique elements that are not in the another set passed as a parameter.
set.difference_update() Updates the set on which the method is called with the elements that are common in another set passed as an argument.
set.discard() Removes a specific element from the set.
set.intersection() Returns a new set with the elements that are common in the given sets.
set.intersection_update() Updates the set on which the instersection_update() method is called, with common elements among the specified sets.
set.isdisjoint() Returns true if the given sets have no common elements. Sets are disjoint if and only if their intersection is the empty set.
set.issubset() Returns true if the set (on which the issubset() is called) contains every element of the other set passed as an argument.
set.pop() Removes and returns a random element from the set.
set.remove() Removes the specified element from the set. If the specified element not found, raise an error.
set.symmetric_difference() Returns a new set with the distinct elements found in both the sets.
set.symmetric_difference_update() Updates the set on which the instersection_update() method called, with the elements that are common among the specified sets.
set.union() Returns a new set with distinct elements from all the given sets.
set.update() Updates the set by adding distinct elements from the passed one or more iterables.
Want to check how much you know Python?