Python: Set Comprehensions

Python: Set Comprehensions

ยท

3 min read

In earlier posts we covered Dictionary Comprehensions and List Comprehensions. As you probably have guessed, the biggest difference with a Set Comprehension is we're creating a Set instead. Since the List and Set being so similar, using that post as a starting point this should be quick. ๐Ÿ˜ƒ

Creating a new set using a for loop looks very much like how we'd create a List.

numbers_list = [1, 2, 3, 4, 5, 6, 7, 8]
new_set = set()

for num in numbers_list:
  new_set.add(num)

We don't need to use an old set to create a new set. As seen here we can use a list and add each to a new set. Of course a shorter method would be new_set = set(numbers_list).

Here is the Set Comprehension of the for loop above.

numbers_list = [1, 2, 3, 4, 5, 6, 7, 8]
new_set = { num for num in numbers_list }

We are using curly braces { } just like when making a Dictionary Comprehension. How does Python know we want a Set instead of a Dictionary? The expression part of the Comprehension returns a single value instead of a key:value pair.

Modifying the Value

With a Set Comprehension we can modify the expression being added to the new set.

Here we'll make a new set that has the values double of the original list.

new_set = { num * 2
  for num in numbers_list }

Filtering the Set

Let's filter the list so we include only even numbers.

new_set = { num
  for num in numbers_list
  if num%2 == 0 }

Examples

The difference between a list and set is mainly that a set won't contain duplicate values. We can use that to remove duplicates without adding extra checks to out code.

Price Point

Let's say we have a list of prices and we want to know the dollar price points of the prices. We don't want duplicates.

prices = [1.49, 1.99, 3.49, 4.25, 3.99, 1.25]

What we want returned is {1, 3, 4}.

price_points = { int(price)
  for price in prices }

The int() will give us the dollar amount but also would give use duplicates. Using a set removes those duplicates without checks written by us.

Product Colors

We've been given a list that contains a dictionary of products. Our task is to provide a set of colors.

products = [ 
  { 'model': 'AVE-101', 'color': 'blue' },
  { 'model': 'QRE-333', 'color': 'gold' },
  { 'model': 'AVE-101', 'color': 'green' },
  { 'model': 'QRE-333', 'color': 'blue' }
]

Looping over the dictionary we'll insert the color value into the set and let the set handle removing the duplicates.

product colors = { product['color']
  for product in products }

Top Salespeople

We have a list of dictionaries and want to know which salespeople that have had sales above $1,000.

sales = [
  {'Employee': 'Aang', 'Amount': 985},
  {'Employee': 'Sokka', 'Amount': 1444},
  {'Employee': 'Katara', 'Amount': 135},
  {'Employee': 'Sokka', 'Amount': 135},
  {'Employee': 'Aang', 'Amount': 1339},
  {'Employee': 'Sokka', 'Amount': 135},
]
top_sales = {
  sale['Employee']
  for sale in sales
  if sale['Amount'] > 1000
}

Resources

Python Reference ReadTheDocs : Set Comprehensions

Python.org : Data type Set

Did you find this article valuable?

Support Russ Eby by becoming a sponsor. Any amount is appreciated!