🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Set Comprehensions

AI & DATA SCIENCE // set-comprehensions

A set comprehension builds a new set in a single expression, automatically discarding duplicate results, with an optional filtering condition.

Syntax

{expression for item in iterable if condition}

Deep Dive Course

A set comprehension looks almost identical to a list comprehension, but uses curly braces instead of square brackets, and, like any set, automatically deduplicates its results — if the expression produces the same value for two different items, only one copy ends up in the final set. It's useful whenever you're transforming a collection and specifically want unique results, without needing a separate call to the set constructor afterward.

1Understanding Set Comprehensions

A set comprehension looks almost identical to a list comprehension, but uses curly braces instead of square brackets, and, like any set, automatically deduplicates its results — if the expression produces the same value for two different items, only one copy ends up in the final set. It's useful whenever you're transforming a collection and specifically want unique results, without needing a separate call to the set constructor afterward.

💡

Don't confuse an empty pair of curly braces with an empty set — a genuinely empty {} is always a dict, not a set; you need the set() constructor explicitly, or a non-empty set comprehension, to get an empty or computed set.

editor.html
words = ["apple", "banana", "cherry", "date"]
first_letters = {word[0] for word in words}
print(first_letters)
localhost:3000

2Practical Example

Here is a real-world application of Set Comprehensions showing how it is used in production Python code.

editor.html
numbers = [1, 2, 2, 3, 4, 4, 5]
remainders = {n % 3 for n in numbers}
print(remainders)
localhost:3000

3Best Practices

Follow these guidelines when working with Set Comprehensions:

1. Use a set comprehension instead of a list comprehension wrapped in set() when you know you only need unique results and don't need list ordering along the way

2. Reach for a set comprehension for fast membership testing on the result, since a set's membership check is average O(1) versus O(n) for a list

3. Remember curly-brace syntax with just an expression, no colon, makes a set comprehension, while adding a colon and a second expression makes a dict comprehension instead

⚠️

Tip: Don't confuse an empty pair of curly braces with an empty set — a genuinely empty {} is always a dict, not a set; you need the set() constructor explicitly, or a non-empty set comprehension, to get an empty or computed set.

editor.html
words = ["apple", "banana", "cherry", "date"]
first_letters = {word[0] for word in words}
print(first_letters)
localhost:3000

Examples

Example 01Basic Usage
words = ["apple", "banana", "cherry", "date"]
first_letters = {word[0] for word in words}
print(first_letters)
Example 02Advanced Example
numbers = [1, 2, 2, 3, 4, 4, 5]
remainders = {n % 3 for n in numbers}
print(remainders)

Best Practices

  • Use a set comprehension instead of a list comprehension wrapped in set() when you know you only need unique results and don't need list ordering along the way
  • Reach for a set comprehension for fast membership testing on the result, since a set's membership check is average O(1) versus O(n) for a list
  • Remember curly-brace syntax with just an expression, no colon, makes a set comprehension, while adding a colon and a second expression makes a dict comprehension instead

Interview Question

What's the key visual difference between a set comprehension and a dictionary comprehension, given they both use curly braces?

Hint: Think about what appears between the braces.

A set comprehension contains a single expression before the for clause, producing one value per item. A dict comprehension contains two expressions separated by a colon, producing a key and a value per item. The presence of that colon inside the braces is what tells Python, and a reader, which of the two comprehension types is being written.

Exercises

MediumPractice using Set Comprehensions in a real scenario.
View Solution
words = ["apple", "banana", "cherry", "date"]
first_letters = {word[0] for word in words}
print(first_letters)

Frequently Asked Questions

What's the key visual difference between a set comprehension and a dictionary comprehension, given they both use curly braces?

A set comprehension contains a single expression before the for clause, producing one value per item. A dict comprehension contains two expressions separated by a colon, producing a key and a value per item. The presence of that colon inside the braces is what tells Python, and a reader, which of the two comprehension types is being written.

Related Functions

setslist-comprehensionsdictionary-comprehensions