Python Sets
In Python, sets are collections that store an unordered and unique set of elements.
set([element1, element2, ... , element_n]);
Alternatively, you can use curly braces without the set keyword.
{ element1, element2, ... , element_n }
Sets support all standard set operations such as union, difference, intersection, and symmetric difference.
Note: When you pass a string as a parameter to the set function, it creates a set of unique characters from the string. For example, in the name "Andrea," the letter "a" appears twice, but it will only appear once in the set.
A Practical Example of Sets
In this example, I assign three city names to the variable city
:
city = set(["Milan", "Rome", "Turin"])
You can also make the assignment without using the set keyword:
city = { "Milan", "Rome", "Turin" }
Next, I assign three European capitals to the variable capitals
:
capitals = set(["Rome", "Paris", "London"])
Now, we have two set variables.
We can use these two sets to perform all the main set operations.
Operation | Operator | Example |
Union (OR) | | | city | capitals |
Intersection (AND) | & | city & capitals |
Difference (Asymmetric Difference) | - | city - capitals |
Symmetric Difference (XOR) | ^ | city ^ capitals |
Union of Sets
To create a union of two sets, use the |
symbol, which is the logical OR operator.
print(city | capitals)
The result of this operation is:
{'Turin', 'Milan', 'Paris', 'London', 'Rome'}
Although "Rome" is in both sets, it appears only once in the union set.
Alternatively, you can use the union()
method:
city.union(capitals)
The result is the same.
Intersection of Sets
To find elements present in both sets, use the AND (&
) operator.
print(city & capitals)
The result of this operation is:
{"Rome"}
"Rome" is the only element present in both sets.
Alternatively, you can use the intersection()
method:
city.intersection(capitals)
The result is the same.
Difference between Sets
To find elements in the first set that are not in the second (set difference or asymmetric difference), write:
print(capitals - city)
The result of this operation is:
{"Paris", "London"}
"Rome" is excluded because it is also in the second set (city
).
Alternatively, you can use the difference()
method:
capitals.difference(city)
The result is the same.
What is asymmetric difference? Asymmetric difference compares two sets and identifies elements that are present in only one of the sets.
Symmetric Difference (XOR)
To find elements that belong to one set but not both, use the ^
symbol for symmetric difference (XOR).
print(capitals ^ city)
The result of this operation is:
{'Turin', 'Milan', 'Paris', 'London'}
"Rome" is excluded because it is present in both sets.
Alternatively, you can use the symmetric_difference()
method:
capitals.symmetric_difference(city)
The result is the same.
Note: The methods union()
, intersection()
, difference()
, and symmetric_difference()
have an advantage over operators, as they can be used between a set and other iterable objects. Conversely, operators can only be used between two set variables.
Differences between Lists and Sets in Python
Both lists and sets can be modified.
However, a set cannot contain duplicate values.
In contrast, a list can contain repeated values.
How do you distinguish a list from a set? In a set, values are assigned using round brackets outside and square brackets inside. In a list, only square brackets are used, while in a tuple, only round brackets are used.
Set Comprehension
List comprehension can also be applied to sets.
Unlike lists, the result for sets does not contain duplicate values.
Example
In this script, I select all the remainders of the division by two.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
remainders = {n % 3 for n in numbers}
The result of this operation is:
{0, 1, 2}
The remainders of the division by 3 for numbers from 1 to 9 are 0, 1, and 2, without duplicates.
How to Check if a Value is in a Set
To check if a specific value is in a set, you can use the in
operator:
x in set_name
Where x
is the element to search for in the set.
This is a boolean expression. If the element exists in the set, the expression is true; otherwise, it is false.
Example
In these two lines of code, I define the set city
and check if the element "Rome" is in the set.
city = set(["Milan", "Rome", "Turin"])
if ("Rome" in city):
print("exists")
The element "Rome" exists in the city set. Therefore, the program output is:
exists
And so on.