PYTHON

Python Set Comprehension: Syntax, Usage, and Examples

Set comprehension lets you build a set in a single readable line. It’s a clean way to transform values and automatically remove duplicates at the same time.


How to Use Set Comprehension in Python

A set comprehension looks a lot like a list comprehension, but it uses curly braces {} instead of square brackets [].

Basic syntax

{expressionfor itemin iterable}

  • expression: the value you want to store in the set
  • item: each element you get from the iterable
  • iterable: something you can loop over, like a list, tuple, string, or range

Here’s a simple example that creates a set of squares:

squares = {n * nfor ninrange(1,6)}
print(squares)# {1, 4, 9, 16, 25}

Adding a condition

You can filter items with an if clause at the end:

{expressionfor itemin iterableif condition}

Example, keep only even squares:

even_squares = {n * nfor ninrange(1,11)if n %2 ==0}
print(even_squares)# {4, 16, 36, 64, 100}

Using an if/else expression

If you want different output depending on a condition, put the conditional inside the expression:

labels = {"even"if n %2 ==0else"odd"for ninrange(1,6)}
print(labels)# {'even', 'odd'}

Notice something funny here. You’ll only get {"even", "odd"} because sets automatically remove duplicates.

Set comprehension vs set() with a loop

You can build a set with a loop:

numbers = [1,2,2,3,3,3]
result =set()

for nin numbers:
    result.add(n)

print(result)# {1, 2, 3}

Set comprehension does the same job in fewer lines:

result = {nfor nin numbers}
print(result)# {1, 2, 3}


When to Use Set Comprehension

Set comprehension works best when you want a clean transformation and you also want duplicates to disappear without extra effort.

1) Removing duplicates while transforming values

Sometimes you don’t just want unique values, you want unique transformed values.

Example: normalize a list of cities by lowercasing them:

cities = ["Vienna","vienna","Podgorica","PODGORICA"]
normalized = {city.lower()for cityin cities}

print(normalized)# {'vienna', 'podgorica'}

A list comprehension would keep duplicates unless you add another step.

2) Building a set for fast membership checks

Sets shine when you need to check “is this value inside?” many times.

Example: create a set of banned words so checks are quick:

banned_words = {"spam","scam","fake"}

You can even generate the set from raw input:

raw_words = ["Spam","Scam","FAKE","spam"]
banned_words = {word.lower()for wordin raw_words}

3) Cleaning input data

Data is often messy, with different spelling, casing, or unwanted whitespace.

Example: turn raw tags into a clean set:

raw_tags = ["  Python","python "," Data ","DATA"]
tags = {tag.strip().lower()for tagin raw_tags}

print(tags)# {'python', 'data'}

4) Creating a unique collection from nested data

A common beginner-to-intermediate task is extracting values from a list of dictionaries.

Example: get all unique roles from a list of users:

users = [
    {"name":"Ava","role":"admin"},
    {"name":"Sam","role":"editor"},
    {"name":"Kai","role":"admin"}
]

roles = {user["role"]for userin users}
print(roles)# {'admin', 'editor'}


Examples of Set Comprehension

Let’s walk through a few practical examples you could run into in real projects.

Example 1: Get unique email domains

Imagine you have a list of emails and you want to know which domains appear.

emails = [
"nora@example.com",
"liam@company.org",
"sofia@example.com",
"jay@school.edu"
]

domains = {email.split("@")[1]for emailin emails}
print(domains)# {'example.com', 'company.org', 'school.edu'}

This is useful for analytics, reporting, or basic validation.


Example 2: Build a set of safe usernames

Usernames might come in with extra spaces, inconsistent casing, or duplicates.

raw_usernames = ["  Mira","mira","MIRA ","Alex","alex"]
usernames = {name.strip().lower()for namein raw_usernames}

print(usernames)# {'mira', 'alex'}

Now you have a clean set you can compare against when someone signs up.


Example 3: Filter product IDs by rule

Suppose you want only the IDs that match a condition, like IDs above 1000.

product_ids = [998,1001,1002,999,1500]

valid_ids = {pidfor pidin product_idsif pid >=1000}
print(valid_ids)# {1001, 1002, 1500}

This works well when you care about uniqueness and don’t want repeats.


Example 4: Create a set of characters from a string

A string is an iterable too. A set comprehension can grab all distinct characters.

word ="programming"
letters = {chfor chin word}

print(letters)# {'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n'}

This can be a quick trick for simple text analysis.


Example 5: Extract unique skills from nested lists

Let’s say you have a list of team members, and each member has a list of skills.

team = [
    {"name":"Elena","skills": ["python","sql"]},
    {"name":"Ravi","skills": ["javascript","python"]},
    {"name":"Maya","skills": ["sql","excel"]}
]

all_skills = {skillfor personin teamfor skillin person["skills"]}
print(all_skills)# {'python', 'sql', 'javascript', 'excel'}

This example uses a nested loop inside the comprehension, which is common once you get comfortable reading them.


Learn More About Set Comprehension

Set comprehensions are easy to pick up, but a few details help you avoid confusion.

Sets don’t keep order

Sets are unordered collections. If you print a set, the order may look random.

nums = {3,1,2}
print(nums)# could show {1, 2, 3} or another order

If you need the result in a predictable order, convert to a sorted list:

sorted_nums =sorted(nums)
print(sorted_nums)# [1, 2, 3]

Sets remove duplicates automatically

This is the main reason people use set comprehension.

nums = [1,1,2,2,3]
unique_nums = {nfor nin nums}
print(unique_nums)# {1, 2, 3}

If you expected five items and only got three, duplicates are the reason.

Set comprehension vs list comprehension

They look almost identical, but the output behaves differently.

values = [1,1,2,2,3]

as_list = [v *2for vin values]
as_set = {v *2for vin values}

print(as_list)# [2, 2, 4, 4, 6]
print(as_set)# {2, 4, 6}

Choose the structure based on your goal:

  • Use a list when you care about order and duplicates
  • Use a set when you care about uniqueness and fast membership checks

Don’t confuse set comprehension with dictionaries

Curly braces also create dictionaries, so it’s easy to mix them up.

This is a set comprehension:

numbers = {nfor ninrange(3)}

This is a dictionary comprehension:

numbers = {n: n * nfor ninrange(3)}

If you see a colon :, you’re building a dictionary.

Avoid heavy work inside comprehensions

Set comprehension keeps code short, but it can get messy if you cram too much logic in.

Example of something that’s harder to read:

result = {name.strip().lower()for namein raw_namesif nameandlen(name.strip()) >2}

A clearer approach is to use a loop when the logic gets crowded:

result =set()

for namein raw_names:
ifnot name:
continue

    cleaned = name.strip().lower()
iflen(cleaned) >2:
        result.add(cleaned)

Short code is nice, but readable code pays the rent.

Using set comprehension for performance

Set comprehension is often faster than building a set with repeated add() calls, because it runs in optimized internal loops.

That said, the bigger win usually comes from using a set in the first place, since membership checks are fast:

allowed_ids = {1001,1002,1003}

print(1002in allowed_ids)# True

If you did the same check in a list of 10,000 items, it would take longer.


Summary

Set comprehension gives you a quick way to build a set from an iterable, with optional filtering using conditions. Use it when you want to transform data and remove duplicates at the same time, like cleaning inputs, extracting distinct values, or creating collections for fast lookups.