Set Comprehension
noun
say it: “SET kom-prih-HEN-shun” /ˈsɛt ˌkɑmprɪˈhɛnʃən/
A one-line way to build a set from another collection, written in curly braces, which keeps each resulting value only once.
What is a set comprehension?
A set comprehension builds a new set from another collection in one line, inside curly braces. Like any set, it keeps each value only once.
orders = ["Latte", "mocha", "latte", "tea"]
drinks = {order.lower() for order in orders}
print(drinks) # {'latte', 'mocha', 'tea'} (in any order)
In a sentence
- “A set comprehension gave her every city her customers live in, with no city twice.”
- “He swapped the loop for a set comprehension that lowercases each email.”
Level: Journeyman · More Python terms at this level
Updated