Python
Python Sets

A set is an unordered collection of items. Every element is unique (no duplicates) and must be immutable (which cannot be changed).

However, the set itself is mutable. We can add or remove items from it.

Sets can be used to perform mathematical set operations like union, intersection, symmetric difference etc.

How to create a set?

A set is created by placing all the items (elements) inside curly braces {}, separated by comma or by using the built-in function set().

It can have any number of items and they may be of different types (integer, float, tuple, string etc.). But a set cannot have a mutable element, like list, set or dictionary, as its element.

>>> # set of integers
>>> my_set = {1, 2, 3}
>>> print(my_set)
{1, 2, 3}
>>> my_set = {1.0, "Hello", (1, 2, 3)}
>>> print(my_set)
{1.0, (1, 2, 3), 'Hello'}
>>> 

Sets are a powerful tool in Python since they have the ability to calculate differences and intersections between other sets. For example, say you have a list of participants in events A and B :

>>> a = set(["chanti", "Ramana", "Venkat"])
>>> print(a)
{'chanti', 'Ramana', 'Venkat'}
>>> b = set(["Ramana", "Reddy"])
>>> print(b)
{'Reddy', 'Ramana'}
>>> 

How to remove elements from a set?

A particular item can be removed from set using methods, discard() and remove().

The only difference between the two is that, while using discard() if the item does not exist in the set, it remains unchanged. But remove() will raise an error in such condition.

>>> # initialize my_set
>>> my_set = {1, 2, 3, 4, 5, 6, 7}
>>> print(my_set)
{1, 2, 3, 4, 5, 6, 7}
>>> my_set.discard(6)
>>> print(my_set)
{1, 2, 3, 4, 5, 7}
>>> my_set.remove(2)
>>> print(my_set)
{1, 3, 4, 5, 7}
>>> my_set.discard(2)
>>> print(my_set)
{1, 3, 4, 5, 7}

Python Set Methods

There are many set methods, some of which we have already used above. Here is a list of all the methods that are available with set objects.

Python Set Methods
Method Description
add() Add an element to a set
update() Update a set with the union of itself and others
remove() Remove an element from a set. If the element is not a member, raise a KeyError
copy() Return a shallow copy of a set
difference() Return the difference of two or more sets as a new set
difference_update() Remove all elements of another set from this set
discard() Remove an element from set if it is a member. (Do nothing if the element is not in set)
clear() Remove all elements form a set
intersection() Return the intersection of two sets as a new set
intersection_update() Update the set with the intersection of itself and another
isdisjoint() Return True if two sets have a null intersection
issubset() Return True if another set contains this set
issuperset() Return True if this set contains another set
pop() Remove and return an arbitary set element. Raise KeyError if the set is empty
symmetric_difference() Return the symmetric difference of two sets as a new set
symmetric_difference_update() Update a set with the symmetric difference of itself and another
union() Return the union of sets in a new set