Python Set: Creating a Set
This page shows how to create sets in Python, which syntax to use, and what happens with duplicates and empty sets.
A set is useful when you want a collection of unique values. Unlike a list, a set does not keep duplicate items.
Quick answer #
numbers = {1, 2, 3}
words = set(["apple", "banana", "apple"])
empty_set = set()
print(numbers)
print(words)
print(empty_set)
Output:
{1, 2, 3}
{'banana', 'apple'}
set()
Use curly braces with values for a non-empty set. Use set() for an empty set.
What this page covers #
- How to create a set with curly braces
- How to create a set with
set() - How to create an empty set correctly
- What happens to duplicate values
- How set creation differs from list, tuple, and dictionary creation
What a set is #
A set is a collection of unique values.
Important things to know:
- Sets do not keep duplicate items
- Sets are unordered
- Sets do not use positions like list indexes
- Sets are useful when you only care whether a value exists
If you want a full beginner explanation, see Python sets explained or what is a set in Python.
Create a set with curly braces #
Use curly braces with comma-separated values to create a non-empty set.
numbers = {1, 2, 3}
print(numbers)
Possible output:
{1, 2, 3}
This is the shortest way to create a set when you already know the values.
Duplicates are removed automatically #
If you repeat a value, Python keeps only one copy.
numbers = {1, 2, 2, 3, 3, 3}
print(numbers)
print(len(numbers))
Possible output:
{1, 2, 3}
3
Even though 2 and 3 appeared more than once, the set stores each value only once.
Create a set with set() #
Use set() when you want to build a set from another iterable.
Common inputs include:
- Lists
- Tuples
- Strings
range()
Create a set from a list #
numbers = set([1, 2, 2, 3, 3, 4])
print(numbers)
Possible output:
{1, 2, 3, 4}
This is a common way to remove duplicates from a list. For a full task-based example, see how to remove duplicates from a list in Python.
Create a set from a tuple #
values = set((10, 20, 20, 30))
print(values)
Possible output:
{10, 20, 30}
Create a set from a string #
letters = set("hello")
print(letters)
Possible output:
{'h', 'e', 'l', 'o'}
The string is treated as an iterable of characters, so the set contains unique letters only.
How to create an empty set #
To create an empty set, use set().
empty_set = set()
print(empty_set)
print(type(empty_set))
Output:
set()
<class 'set'>
Do not use {} #
Empty braces do not create a set.
empty_value = {}
print(empty_value)
print(type(empty_value))
Output:
{}
<class 'dict'>
{} creates an empty dictionary, not an empty set.
What happens with duplicates #
Sets store each value only once.
If the same value appears multiple times, Python removes the extra copies when the set is created.
words = {"apple", "banana", "apple", "apple", "orange"}
print(words)
print(len(words))
Possible output:
{'banana', 'orange', 'apple'}
3
This is why sets are helpful for cleaning repeated data.
Values you can put in a set #
Set items must be hashable. In simple terms, that means Python must be able to treat the value as stable and usable inside a set.
These usually work:
- Numbers
- Strings
- Tuples
- Booleans
values = {1, "apple", (10, 20), True}
print(values)
These do not work directly:
- Lists
- Dictionaries
bad_set = {[1, 2, 3]}
This raises an error:
TypeError: unhashable type: 'list'
Another example:
bad_set = {{"name": "Sam"}}
This also raises an error because dictionaries are unhashable.
Set creation examples beginners need #
Create a set of numbers #
numbers = {1, 2, 3, 4}
print(numbers)
Create a set from a list with duplicates #
items = [1, 1, 2, 3, 3]
unique_items = set(items)
print(unique_items)
Create a set from a string #
letters = set("banana")
print(letters)
Create an empty set #
empty_set = set()
print(empty_set)
Common mistakes #
Here are some common problems beginners run into when creating sets.
Using {} and expecting an empty set #
This is one of the most common mistakes.
value = {}
print(type(value))
This prints:
<class 'dict'>
Use set() instead.
Forgetting that duplicate values are removed #
If you create a set with repeated values, the duplicates disappear.
numbers = {1, 1, 2, 2, 3}
print(numbers)
You will only get the unique values.
Expecting set items to stay in a fixed order #
A set is unordered. You should not rely on item position.
This means code like this does not work:
numbers = {10, 20, 30}
# numbers[0] # This would cause an error
If you need ordered items by position, use a list instead.
Trying to put a list or dictionary inside a set #
Lists and dictionaries cannot be added directly to a set because they are unhashable.
# bad = {[1, 2]}
# bad = {{"a": 1}}
Both examples would raise a TypeError.
Confusing set creation with dictionary creation #
These look similar, but they are different:
my_set = {1, 2, 3}
my_dict = {"a": 1, "b": 2}
print(type(my_set))
print(type(my_dict))
Useful checks while debugging #
If you are not sure what Python created, these quick checks help:
my_set = set([1, 2, 2, 3])
print(type(my_set))
print(my_set)
print(len(my_set))
print({})
print(set())
Use these to confirm:
- The object type
- The actual values stored
- Whether duplicates were removed
- The difference between
{}andset()
FAQ #
How do you create a set in Python? #
Use curly braces for a non-empty set, like {1, 2, 3}, or use set() with another iterable.
How do you create an empty set in Python? #
Use set(). Empty braces {} create a dictionary, not a set.
Does a set keep duplicates? #
No. Python removes duplicate values automatically when the set is created.
Can a set contain a list? #
No. Lists are mutable and unhashable, so they cannot be stored inside a set.
Is a set ordered in Python? #
No. A set is unordered, so you should not rely on item position.
See also #
- Python sets explained
- Python
set.add()method - Python
set.update()method - How to remove duplicates from a list in Python
- What is a set in Python?
Next, learn how to add items to a set or use a set to remove duplicates from data.