SyntaxStudy
Sign Up
Python Beginner 3 min read

Set Comprehensions

Set Comprehensions

Set comprehensions use curly braces like dict comprehensions but without a colon — they produce a set with unique values.

Example
nums = [1, 2, 2, 3, 3, 3]
unique_squares = {x**2 for x in nums}
print(unique_squares)
# {1, 4, 9}
Pro Tip

Set comprehensions automatically deduplicate results.