A set in Python is a collection that holds unique values with no duplicates and no fixed order
A set is one of Python's built-in data types, similar to a list or dictionary but with one key difference: it automatically removes duplicates and does not keep items in any particular order. If you add the same value twice, Python keeps only one copy. Sets are useful when you need to track which items exist without caring about the sequence, or when you want to quickly check whether something is in your collection.
You create a set by putting values inside curly braces with commas between them, or by using the set() function. Once you have a set, you can add items, remove items, and perform operations like finding what two sets have in common or what makes them different.
Key Takeaways
- Create a set by typing curly braces with comma-separated values, like {1, 2, 3}, or use set() to convert a list or other collection into a set.
- Sets automatically remove duplicates, so {1, 1, 2} becomes {1, 2} with no extra work from you.
- Add items with .add() and remove them with .remove() or .discard(), which differ in how they handle missing items.
- Use set operations like .intersection(), .union(), and .difference() to compare two sets and find what they share or what makes them unique.
- Check whether a value exists in a set using the in keyword, which is faster with sets than with lists when dealing with large collections.
Creating a set with curly braces or the set() function
The simplest way to create a set is to type values inside curly braces. Open a Python editor or the Python shell and type:
my_set = {1, 2, 3, 4, 5}
Python now has a set called my_set containing five numbers. You can also create a set of strings, mixed types, or even an empty set — but here is the catch: you cannot use empty curly braces {} to make an empty set, because Python treats that as an empty dictionary instead. To create an empty set, use the set() function:
empty_set = set()
The set() function also converts other collections into sets. If you have a list with duplicate values and want to remove them all at once, pass the list to set():
my_list = [1, 2, 2, 3, 3, 3, 4] my_set = set(my_list) print(my_set) # Output: {1, 2, 3, 4}
Notice that the duplicates are gone. You can also convert a string into a set, which splits it into individual characters:
my_set = set("hello") print(my_set) # Output: {'h', 'e', 'l', 'o'}
The letter 'l' appears only once in the set even though it appears twice in the word "hello".
Adding and removing items from a set
Once you have created a set, you can add new items using the .add() method. Type the set name, a dot, then add() with the value you want to add inside the parentheses:
my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
If you try to add a value that already exists, nothing happens — the set stays the same because duplicates are not allowed. To add multiple items at once, use the .update() method and pass it a list, another set, or any collection:
my_set = {1, 2, 3} my_set.update([4, 5, 6]) print(my_set) # Output: {1, 2, 3, 4, 5, 6}
To remove items, you have two choices: .remove() and .discard(). Both delete the item, but they behave differently if the item does not exist. .remove() will stop your program and show an error, while .discard() does nothing if the item is not there:
my_set = {1, 2, 3} my_set.remove(2) print(my_set) # Output: {1, 3} my_set.discard(5) # No error, even though 5 is not in the set
Use .remove() when you are certain the item exists and want to catch mistakes. Use .discard() when you are not sure and want the program to keep running.
Checking whether a value is in a set
To test whether a value exists in a set, use the in keyword. This is one of the main reasons sets are useful — checking membership is very fast, even with thousands of items:
my_set = {1, 2, 3, 4, 5} if 3 in my_set: print("3 is in the set") else: print("3 is not in the set")
You can also use not in to check the opposite:
if 10 not in my_set: print("10 is not in the set")
With a list, checking membership works the same way, but it gets slower as the list grows because Python has to look at each item one by one. With a set, Python uses a faster method behind the scenes, so in stays fast no matter how many items you have.
Comparing sets with intersection, union, and difference
Sets have special methods that let you compare two sets and find relationships between them. The .intersection() method finds items that appear in both sets:
set_a = {1, 2, 3, 4} set_b = {3, 4, 5, 6} common = set_a.intersection(set_b) print(common) # Output: {3, 4}
The .union() method combines both sets and removes duplicates:
combined = set_a.union(set_b) print(combined) # Output: {1, 2, 3, 4, 5, 6}
The .difference() method finds items in the first set that are not in the second set:
unique_to_a = set_a.difference(set_b) print(unique_to_a) # Output: {1, 2}
You can also use symbols as shortcuts: & for intersection, | for union, and - for difference. These do the same thing but take less typing:
common = set_a & set_b # Same as intersection combined = set_a | set_b # Same as union unique = set_a - set_b # Same as difference
Common mistakes when working with sets
The most frequent mistake is trying to create an empty set with {}. This creates a dictionary, not a set, and you will not realize the problem until you try to use set methods. Always use set() for an empty set.
Another common error is assuming sets have an order. They do not. If you print a set multiple times, the items might appear in a different order each time. If order matters for what you are building, use a list instead.
A third mistake is trying to add a list or dictionary to a set. Sets can only hold values that do not change, called immutable types — numbers, strings, and tuples work fine, but lists and dictionaries do not. If you need to store a collection inside a set, convert it to a tuple first:
my_set = set() my_set.add((1, 2, 3)) # Tuple works print(my_set) # Output: {(1, 2, 3)}
Finally, remember that .add() takes only one item at a time. If you try to add multiple items with .add(), it will not work as expected. Use .update() instead.
When to use a set instead of a list or dictionary
Use a set when you need to store unique values and do not care about order. Common situations include tracking which users have visited a page, finding duplicate entries in data, or checking membership quickly in a large collection.
Use a list when order matters or when you need to store duplicates. Use a dictionary when you need to pair keys with values, like storing a person's name with their age.
If you are unsure whether you need a set, ask yourself: do I care about the order of items, and do I need to store the same value more than once? If the answer to both is no, a set is probably the right choice.
Frequently Asked Questions
Can I change items in a set after I create it?
No, you cannot change an item that is already in a set. You can only remove it and add a different value. If you need to modify items, use a list instead. However, you can add new items and remove existing ones freely.
What is the difference between .remove() and .discard()?
Both delete an item from the set. The difference is that .remove() stops your program with an error if the item does not exist, while .discard() does nothing. Use .remove() when you expect the item to be there and want to catch mistakes. Use .discard() when you are not sure.
Why is checking membership faster in a set than in a list?
Sets use a technique called hashing that lets Python find an item almost instantly, no matter how many items are in the set. Lists have to check each item one by one, which gets slower as the list grows. For small collections the difference is tiny, but with thousands of items, sets are much faster.
Can I use a set inside another set?
No, because sets can only hold immutable types — values that do not change. A set is mutable, so you cannot nest one inside another. If you need to store a collection inside a set, convert it to a tuple first, since tuples are immutable.
How do I convert a set back to a list?
Use the list() function and pass the set to it: my_list = list(my_set). Keep in mind that the order will not be the same as the original list if you converted a list to a set and back, because sets do not preserve order.