How to use Sets in Python
In Python, a set
is a data type used to store an unordered collection of unique values. If you need to keep a collection of data that must never have any duplicate values, sets are designed for this task.
In this tutorial, we will learn how to create and use sets in Python using examples.
Create a Set
Firstly, let's create an empty set
. Create a new variable called x
and set its value to set
followed by ()
(parenthesis).
x = set()
Create a Set from a List, Tuple, String or Object
To create a new set using a list, tuple, string or object pass it inside the ()
(parenthesis) of set()
.
x = set([1,2,2,4,5])
print(x)
{1, 2, 4, 5}
Adding Elements to a Set
Now we have defined a new set
in the variable x
, we can add data to it. Type x
followed by .
(dot) followed by add()
. The value to add is put inside the ()
(parenthesis) of add()
.
x.add(1)
print(x)
{1}
If we tried to add another 1
to the list, nothing will be added, but other unique values will be added.
x.add(1)
x.add(1)
x.add(2)
print(x)
{1, 2}
Adding Multiple Items to a Set
To add multiple items to a set
use the update()
method. This will accept any iterable data type. These data types are strings, lists, objects and tuples. in the example below, we are adding a list of numbers to a set.
x = set()
x.update([1,2,3,4])
print(x)
{1, 2, 3, 4}
Iterate over a Set
We can iterate over a set
using a for loop, which is in fact in the same way we would iterate over a list
.
for v in x:
print(v)
1
2
Remove Elements from a Set
To remove elements from a list use either the remove()
or discard()
method, passing the value to remove inside the ()
(parenthesis). The only difference between the two methods is that remove()
will throw an error when trying to remove a value that doesn't exist on the set.
x.remove(1)
print(x)
{2, 3, 4}
Remove the Last Element from a Set
To remove the last element from a set
use the pop()
method. Since sets are an unordered data type you won't know what element is at the end of the object making pop()
a little redundant.
x.pop()
Remove All Elements from a Set
To remove all the elements from a set
, use the clear()
method.
x.clear()
print(x)
set()
Remove Duplicates from a List
We can convert a list with duplicate values into a set
to remove duplicates then loop through the set
and append the values to a new list.
dups = [1,1,2,2,3,3]
unique = set(dups)
new = []
for e in unique:
new.append(e)
print(new)
[1, 2, 3]
Check if a Value Exists in a Set
To check if a value exists in a set
type the value followed by in
then the name of the set. It will return True
or False
.
x = set([1,2,2,4,5])
print(1 in x)
True
Join Sets Together using union()
To join sets together use either a |
(pipe) or use the built-in union()
method.
x = set([1,2,2,4,5])
y = set([1,2,2,6,6])
z = x | y
print(z)
{1, 2, 4, 5, 6}
Or with union()
:
x.union(y)
Intersect Sets
An intersection will return values that are found in both sets. This is done using an &
(ampersand) or the intersection()
method.
x = set([1,2,2,4,5])
y = set([1,2,2,6,6])
z = x & y;
print(z)
{1, 2}
Or with intersection()
:
z = x.intersection(y)
Set Difference
The difference is the values from the first set that are in the first set and not second. This is done using a -
(dash) or with the difference()
method.
x = set([1,2,2,4,5])
y = set([1,2,2,6,6])
z = x - y
print(z)
{4, 5}
Or with difference()
:
z = x.difference(y)
Symmetric Set Difference
The symmetric difference is the values from both sets that are not in both sets. This can be done using a ^
(caret) or the symmetric_difference()
method.
x = set([1,2,2,4,5])
y = set([1,2,2,6,6])
z = x ^ y
print(z)
Or using the symmetric_difference()
method:
z = x.symmetric_difference(y)
How to Create an Immutable Set
To create an immutable set
in Python, create the set using the frozenset()
method. The list
it creates can still be used with methods that don't change the values on the set.
x = frozenset([1,2,2,4,5])
print(x)
frozenset({1, 2, 4, 5})
All the Set Methods
For reference here are all the methods available to use with a set
.
add()
- add an element to theset
copy()
- copy the wholeset
pop()
- remove the last itemremove()
- remove a valuediscard()
- remove a valuedifference()
- returns a new set from the difference of two setsclear()
- remove all elementsdifference_update()
- remove all elements from another set from aset
intersection()
- intersect two setsintersection_update()
- update set with intersection with anotherset
isdisjoint()
- returns True if intersection result is nullissubset()
- returns True if set is contained in the otherissuperset()
- returns True if the set contains nested setssymmetric_difference()
- returns symmetric difference of setssymmetric_difference_update()
- update symmetric difference to aset
union()
- create a union of setsupdate()
- add one of many elements to aset
Built-in Set Functions
Here are all the native functions that can be used with sets.
all()
- returnsTrue
if the set is empty or all its values are trueany()
- if any value isTrue
len()
- get the number of elements in a setmax()
- get the largest valuemin()
- get the smallest valuesorted()
- create a sortedlist
based on aset
sum()
- sum of all the values in aset
enumerate()
- get an enumerable object containing indexes and values
Conclusion
You now know how to use sets in Python and how they can be used to remove duplicates from lists.