The technique that @thepitycoder presented works well by getting inside the tuples to perform the count.
Another option is to unpack each tuple by specifying two variables within the
for clause of the list comprehension, as follows:
num_two_dollar_slices = len([price for price, topping in pizzas if price == 2])
The following conforms more closely to the instructions by literally counting the number of occurrences of
2 in the list, but that renders it a bit clumsy:
num_two_dollar_slices = [price for price, topping in pizzas if price == 2].count(2)
If you would like to generalize the process as a function, this will perform the task:
return len([price for price, topping in pizzas if price == price_to_count])
# count $1.00 slices
num_one_dollar_slices = price_count(1)
# count $2.00 slices
num_two_dollar_slices = price_count(2)
This post was edited on July 8, 2019 to refine some of the descriptive text.