Sets in Python
Overview
Sets in Python are a collection of unique elements that are unordered and immutable. In other words, sets can be thought of as a collection of distinct items that cannot be modified once created. Python provides a built-in set data type that allows users to create, manipulate and perform operations on sets efficiently. Sets in Python are very useful for various applications such as removing duplicates from a list, performing mathematical operations like union, intersection and difference on sets, etc..
What is Set in Python?
In mathematics, a set is a collection of distinct objects forming a group. The objects can be of any type like numbers, names of movies, company names, etc. There can be any number of objects in a set and the objects can be of different data types. Set is a concept of Set Theory which is a popular and useful topic of mathematics. There are several operations that can be performed on a set, these operations include union, intersection, difference.
Sets in python are also similar but with a few conditions.
- First, that a set cannot contain duplicate elements.
- Second, the elements of a set are immutable(once entered cannot be changed) but the set as a whole is mutable.
In python, operations like union, intersection can be performed on a set, but it is also used for other purposes which we will discuss in this article.
The formal definition of a set in Python will be: Set is an unordered collection of items, where every element is unique and immutable. However, the set itself is mutable.
:::
Creating Sets in Python
Just like most other things, creating a set in Python is really simple. There are two ways of creating a set
- Putting all the elements inside curly braces “{“ and “}”
- Using set() method.
If the curly braces look familiar to you, it's because they are used in Python Dictionaries.
Using Curly Braces
Let's look at an example.
Output:
Here you can see we have created a set that contains only numbers and another which contains numbers and strings.
Also, click here to learn more about Numbers in Python.
Using set() function
Syntax: set([iterable])
Return type: Empty is no argument is given otherwise a set or frozenset object
set() takes an iterable as an argument. In python, string, lists, and dictionaries are iterable so you can pass them inside set(). set() returns a set or frozenset object (explained later in the article). If there is no input (i.e. no iterable passed as argument) then an empty set is returned.
Here is an example which uses set() function
Output:
Here is a code example that shows how you can make a set from iterables (lists, dictionaries, string, etc.)
Output:
One important property of set() is that the elements are immutable. So you cannot enter any mutable object inside a set (like dictionaries, lists, set).
Here when mySetOne is executed it gives TypeError: unhashable type: ‘list’
When mySetTwo is executed, we get
Adding Items in a Python Set
Adding elements in a set is not the same as adding elements in lists. Set do not use indexing. There are two methods to add elements in a set
- add() allows adding single element
- update() allows to add multiple elements
Let's take a look at the code -
Output:
update() takes an iterable like string, list, dictionary as an argument.
Removing Items from a Set in Python
For removing elements we have these methods:
- remove(element) - This method removes the element from the set. If the element is not present then it will throw an error.
- discard(element) - This method removes the element from the set. If the element is not present then the set will remain unchanged.
- clear() - This method is used to delete all the set elements.
Let’s take a look at the code to understand these method better.
Output:
Here you can see that the element that is passed to discard() does not exist in the set and discard() did not give any error. Also, this code shows the use of clear().
Set Operations in Python
Most operations on set can be performed in two ways.
- Using operator
- Using methods
We will look at both ways of accomplishing the task.
Union
Union operation combines all the elements from both sets. “|” (pipe) symbol is used to carry out this operation. We can also use the union() method in Python.
Lets look at the code example:
Ouput:
In this example, you can see that union combines all the elements from both sets and it does not include any duplicate elements.
Intersection
Intersection operation picks the elements that are common in both sets. “&” operator or intersection() method is used to perform this operation.
Here in the example, you can see that the intersection result picks the two elements which are common in both the chemicals.
Output:
Difference
The difference in sets is similar to subtraction. When set B is subtracted from set A i.e, Set A - Set B then we say that in the resulting set all the elements of Set B will be removed from Set A. “-” (subtraction sign) or difference() method is used to perform this operation.
Output:
Let's assume that you passed three sets A, B, and C to find the difference. In this case, first, the difference of A and B will be calculated, then its result (suppose R) will be used to calculate the difference with C.
Symmetric Difference
The symmetric difference of two sets A and B is a set that contains elements in either A or B but not both. “^” operator or symmetric_difference() method in python is used to perform this operation.
Output:
Frozen Set
Frozen set is a built-in data type in Python. It is a set that is immutable (once defined, cannot be changed). It supports all non-modifying operations of the set. Operations like add() won’t work in the case of a frozen set.
Here is a simple example:
Output:
A common use case of frozenset can be to store dictionary keys. Here is an example showing that
Output:
Suppose that we want to display this data to some user then using the frozenset makes sense as no one will be able to change them.
Python Built-in set methods
Method | Description |
---|---|
add() | Adds an item to the set if it is not already present. |
clear() | Removes all the elements from the set |
copy() | Returns a copy of the set. |
difference() | Returns a set containing the difference between two or more sets |
remove(item) | Remove the from the set; it must be a member. If the item is not a member, raise a KeyError. |
discard() | Removes the specified item from the set. |
difference_update() | Removes the items in this set that are also included in another, specified set. |
intersection() | Returns a set, that is the intersection of two or more sets |
intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
isdisjoint() | Returns whether two sets have a intersection or not |
issubset() | Returns whether another set contains this set or not |
issuperset() | Returns whether this set contains another set or not |
pop() | Removes an element from the set |
symmetric_difference() | Returns a set with the symmetric differences of two sets |
symmetric_difference_update() | Inserts the symmetric differences from this set and another |
union() | Return a set containing the union of sets |
update() | Update the set with another set, or any other iterable |
Conclusion
In this article, you have gained knowledge of how to create sets in Python and perform various operations on it.
Set like lists, dictionaries are a data type given by Python which has its own use case. It might be helpful in some places while not so much in others. Knowing what you can do with a set will help to judge if it fits your requirement.