How to Declare a Set in Python
What you’ll build or solve
You’ll declare a set in Python, either by creating one with initial values or starting empty.
When this approach works best
A set works best when you want to:
Learn Python on Mimo
- Keep a collection of values with no duplicates, like tags, IDs, or usernames.
- Deduplicate data fast before further processing, like cleaning a list of inputs.
- Do quick membership checks, like “have I seen this value already?”
A set is a bad fit when you need order, duplicates, or index-based access. Use a list for ordered data, or a dictionary for key-value data.
Prerequisites
- Python 3 installed
- Basic understanding of variables and lists
Step-by-step instructions
1) Create a set (with values or empty)
You can declare a set using curly braces with values, or by calling set().
Option A: Create a set with values (most common)
Bash
languages= {"python","javascript","sql"}
print(languages)
print(type(languages))
Option B: Create an empty set
seen=set()
print(seen)
print(type(seen))
What to look for: {} creates an empty dictionary, not an empty set.
Bash
x= {}
print(type(x))# <class 'dict'>
If you want an empty set, always use set().
2) Build a set from an existing collection (dedupe)
If you already have a list (or another iterable), convert it with set() to remove duplicates.
raw_tags= ["python","python","sql","api","sql"]
tags=set(raw_tags)
print(tags)
This works with many iterables, like tuples or strings.
nums= (1,2,2,3)
print(set(nums))# {1, 2, 3}
You can also convert a string into a set of unique characters:
word="banana"
print(set(word))# {'b', 'a', 'n'}
Examples you can copy
Example 1: Remove duplicates from user input
emails= [
"ana@example.com",
"marko@example.com",
"ana@example.com",
"lea@example.com",
]
unique_emails=set(emails)
print(unique_emails)
Example 2: Declare a set of tuples for coordinates
Tuples are hashable, so they work well in sets.
visited= {(42.43,19.26), (43.51,16.44)}
print(visited)
more= [(45.81,15.98), (42.43,19.26)]
visited_all=visited|set(more)
print(visited_all)
Example 3: Merge two sets of allowed features
This keeps only distinct values.
base_features= {"search","export"}
paid_features= {"export","priority_support","team_access"}
all_features=base_features|paid_features
print(all_features)
Common mistakes and how to fix them
Mistake 1: Using {} for an empty set
What you might do
items= {}
Why it breaks
{} creates a dictionary, so set behavior and set methods won’t apply.
Fix
items=set()
print(type(items))
Mistake 2: Trying to put a list or dict inside a set
What you might do
bad= {[1,2,3]}
Why it breaks
Lists and dictionaries are unhashable, so Python can’t store them inside a set.
Corrected approach
good= {(1,2,3)}
print(good)
Troubleshooting
If you see AttributeError: 'dict' object has no attribute 'add', you declared {}. Replace it with set().
If you see TypeError: unhashable type: 'list', convert inner lists to tuples before placing them in a set:
LUA
points= [[1,2], [3,4]]
point_set= {tuple(p)forpinpoints}
print(point_set)
If your set “loses” a value like 1 or True, avoid mixing booleans and integers in the same set. True == 1 and False == 0, so they are treated as duplicates.
If the printed order looks inconsistent, remember that sets are unordered. Sort only when you need display output:
s= {"b","a","c"}
print(sorted(s))
Quick recap
- Declare a set with values using
{...}, like{"python", "sql"} - Declare an empty set with
set(), not{} - Convert an iterable to a set with
set(iterable)to remove duplicates - Use sets when duplicates should disappear, and order does not matter
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot