Python Sets are a collection type which contain an unordered collection of unique and immutable objects. In other words, a Python set can’t hold duplicate items and once a set is created, the items cannot change.

The order is not maintained. For example, everytime you print the same set, the order of items can be different.

In Python, sets are constructed using curly brackets {} and each item in the set is separated by a comma ,.

Like Python lists, sets can also contain multiple different types of objects, so they don’t all need to be strings, or integers. For example, we can have a set containing mixed types:

mixedTypesSet = {'one', True, 13, 2.0}

How to Create a Set

colorsSet = {"red", "green", "blue"}
print(colorsSet)

Output:

{'red', 'blue', 'green'}

How to Access Items of a Set

We cannot use an index to access an item in a set. This is because a set is unordered and doesn’t maintain an index. However, we can use the for loop to iterate through the items in a set.

colorsSet = {"red", "green", "blue"}
for c in colorsSet:
    print(c)

Output:

green
red
blue

How to Add Items to a Set

To add one item to a set we need to use the add() method.

To add more than one item to a set we need to use the update() method.

Adding One Item

colorsSet = {"red", "green", "blue"}
colorsSet.add("yellow")
print(colorsSet)

Output:

{'blue', 'red', 'green', 'yellow'}

Adding More Than One Item

colorsSet = {"red", "green", "blue"}
colorsSet.update(["yellow", "orange", "white"])
print(colorsSet)

Output:

{'white', 'red', 'green', 'yellow', 'orange', 'blue'}

How to Remove an Item From a Set

There are two methods to remove an item from a set: remove() and discard().

remove() method removes the specified item. If the item doesn’t exist, remove() will raise an error.

colorsSet = {"red", "green", "blue", "orange"}
colorsSet.remove("orange")
print(colorsSet)

Output:

{'blue', 'green', 'red'}

discard() method removes the specified item. If the item doesn’t exist, discard() will NOT raise an error.

Remove All Elements of a Set

To remove all elements and empty the set, we use the clear() method:

colorsSet = {"red", "green", "blue", "orange"}
colorsSet.clear()
print(colorsSet)

Output:

set()

Delete a Set Completely

To delete a set completely, use the del keyword:

colorsSet = {"red", "green", "blue", "orange"}
del colorSet
print(colorsSet)

Output:

Traceback (most recent call last):
  File "pythonSet.py", line 78, in <module>
    del colorSet
NameError: name 'colorSet' is not defined

How to Get the Length of a Set

You can get the set length by calling the len() method, e.g.:

colorsSet = {"red", "green", "blue", "orange"}
print(len(colorsSet))

Output:

4

How to Join Two Sets Together

The easiest way to join two sets together is to use the union() method which returns a new set containing items from joined sets.

colorsSet = {"red", "green", "blue", "orange"}
numbersSet = {1, 2, 3, 4}

numbersAndColors = colorsSet.union(numbersSet)
print(numbersAndColors)

Output:

{1, 2, 'blue', 3, 4, 'green', 'red', 'orange'}
See also