Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is set in Python

Understanding the Concept of a Set

When you hear the word "set," you might think of a tennis match or a collection of items. In the world of Python programming, a set is not too different from the latter. A set is a collection of items, but with a few special properties that make it unique and useful in certain situations.

Imagine you have a box of colored balls, and you want to know which unique colors are present. You would go through the box, pick out one of each color, and ignore the duplicates. In Python, this is exactly what a set does: it holds unique items, without any particular order, and it automatically takes care of removing any duplicates for you.

Creating a Set

To create a set in Python, you use curly braces {} or the set() function. Here's how you can create a set of colors:

# Using curly braces
color_set = {'red', 'blue', 'green'}

# Using the set() function
another_color_set = set(['red', 'blue', 'green'])


Both of these lines of code will give you a set with the colors red, blue, and green. If you had any duplicates in the list passed to set(), they would be automatically removed.

Adding and Removing Items

Once you have a set, you might want to add new items or remove existing ones. This is similar to adding or taking out balls from our box of colored balls.

To add an item, you use the add() method:


Now, the color_set includes 'yellow'. If 'yellow' was already in the set, nothing would change, because sets only keep unique items.

To remove an item, you can use the remove() method:


After this, 'red' is no longer in the color_set. But be careful: if the item isn't in the set, remove() will raise an error. To avoid this, you can use the discard() method, which removes the item if it exists, but does nothing if it doesn't.

Checking for Membership

Sometimes you want to check if a particular item is in your set, like searching for a specific colored ball in your box. In Python, you use the in keyword for this:

print('blue' in color_set)  # This will print True if 'blue' is in the set, or False otherwise

Operations with Sets

Sets are not just static containers; you can perform operations that combine them in different ways, similar to mathematical sets you might have learned about in school.


The union of two sets is a set containing all the unique items from both sets. Think of it as pouring two boxes of balls into a larger box and removing any duplicates.

set1 = {'apple', 'banana'}
set2 = {'banana', 'cherry'}

union_set = set1.union(set2)
print(union_set)  # Output: {'apple', 'banana', 'cherry'}


The intersection of two sets is a set containing only the items that are in both sets. Imagine two circles overlapping; the intersection is where they both overlap.

intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {'banana'}


The difference between two sets is a set containing items that are in the first set but not in the second. It's like taking away the balls in the second box from the first box.

difference_set = set1.difference(set2)
print(difference_set)  # Output: {'apple'}

Symmetric Difference

The symmetric difference is a set containing items that are in either of the sets but not in both. It's like combining two sets and then removing the intersection.

symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set)  # Output: {'apple', 'cherry'}

Iterating Over a Set

You can go through each item in a set, just like checking each ball in your box, one by one. This is done using a for loop:

for color in color_set:

This will print each color in the color_set. Remember, sets are unordered, so the order in which the items are printed can vary.

When to Use a Set

Sets are particularly useful when you need to keep track of unique items, and you're not concerned with the order they're stored in. They're also very efficient for checking if an item is present in the collection, which can be handy in many situations, such as when filtering duplicates or testing for membership.


In your journey as a budding programmer, understanding different data structures like sets can greatly enhance your ability to solve problems efficiently. Think of a set as a vigilant gatekeeper, ensuring that no duplicates enter its domain, and offering a treasure trove of operations to manipulate unique collections of items.

As you continue to code, you'll find sets popping up in all sorts of places, from filtering search results to managing tags on a blog. The more you use them, the more you'll appreciate their simplicity and power. So next time you find yourself needing to handle a collection of unique items, remember the humble set – your toolkit for ensuring uniqueness in the Pythonic world.