StackTips

Python: Working with Sets

Updated On: Sep 17, 2022

Sets in Python are similar to list except that they do not allow for duplicate elements inside. Not only that but the collection is un-indexed, meaning there is no concept of indexes at all.

Sets are defined using curly braces.

Creating a Set

Creating a set is similar to creating both a list and tuple:

colors = {"red", "white", "blue"}
print(colors)

Output:

{'white', 'red', 'blue'}

Notice that the order of items printed in the output is different from the order defined. Because sets are unordered and without indexes, the order in which items appear can be in any order.

Adding Items to a Set

While you cannot change the items inside a set, you can still add new items to the set. To do this, use the add() method.

colors = {"red", "white", "blue"}
colors.add("green")
print(colors)

Output:

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

If you want to add multiple items to a set at once, Python also offers this functionality using the update() method:

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

Output:

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

Looping through a Set

Here is how you iterate over a set:

colors = {"red", "white", "blue", "orange"}

for color in colors:
    print(color)orange
white
red
blue

Output:

    white
blue
red

Delete an Item from a Set

In addition to adding an item to a set, you can also remove items from it using the remove function.

colors = {"red", "white", "blue", "orange"}
colors.remove("blue")
print(colors)

Output:

{'orange', 'white', 'red'}

If the item you're trying to remove doesn't exist, an error will be raised.

If you want to safely try to remove an item from a set that you're not sure exists in the set or not, use the discard:

colors = {"red", "white", "blue", "orange"}
colors.discard("green")
print(colors)
{'orange', 'white', 'red', 'blue'}

Check if Item Exists in Set

You can check if an item exists in a set using the in keyword:

colors = {"red", "white", "blue"}

print("red" in colors)
print("yellow" in colors)

Output:

True
False

Set Length

Get the number of items in your set using the len() function:

colors = {"red", "white", "blue"}
print(len(colors))

Output:

3

Combining Multiple Sets

When you have two sets and you want to combine them, Python gives you two options. You can either make a new set or add the items to an existing set. To make a new set entirely, use the union() function:

colors1 = {"red", "white", "blue"}
colors2 = {"red", "purple", "brown"}

colors3 = colors1.union(colors2)
print(colors3)

Output:

{'white', 'brown', 'red', 'purple', 'blue'}

If you don't want to make a new set, and simply want to add the items from one set to another, use the update() function:

colors1 = {"red", "white", "blue"}
colors2 = {"red", "purple", "brown"}

colors1.update(colors2)
print(colors1)

Output:

{'blue', 'purple', 'brown', 'red', 'white'}