# Count a zipped list in python

the last task asks you to count number of two dollar toppings. Initially I tried running count on the zipped list ‘pizzas’, but that gave me a zero. If I run the count on ‘prices’ it works. Can you not use count on a zipped list?

toppings = [‘pepperoni’, ‘pineapple’, ‘cheese’, ‘sausage’, ‘olives’, ‘anchovies’, ‘mushrooms’]
prices = [2, 6, 1, 3, 2, 7, 2]
num_pizzas = len(toppings)
print(‘We sell ’ + str(num_pizzas) + ’ different kinds of pizza!’)
pizzas = list(zip(prices, toppings))
pizzas.sort()
print(pizzas)
cheapest_pizza = pizzas
priciest_pizza = pizzas[-1]
three_cheapest = pizzas[:3]
print(three_cheapest)
num_two_dollar_slices = prices.count(2)
print(num_two_dollar_slices)

Each item in `pizzas` is a tuple consisting of a price and a topping. To use `count` successfully here, you’ll need to supply a matching tuple as an argument. There are three different two-dollar toppings, so you’ll need to call the `count` method three times, once for each of the toppings.

This will work, but it is not very concise …

``````num_two_dollar_slices = pizzas.count((2, "pepperoni")) + pizzas.count((2, "olives")) + pizzas.count((2, "mushrooms"))
print(num_two_dollar_slices)
``````

There are other ways to use the zipped `pizzas` file to do this, but they are not as concise as simply using the `count` method on `prices`.

Edited on November 29, 2018 to account for all three two-dollar toppings

What you might do is filter the list so that only those with a price of 2 remain, and then get the length. You could use a loop, filter, or list comprehension to accomplish that.
Alternatively process each pizza to get the price out, and then count how many 2’s there are in that result

Ideally, Len, as a proprietor of a pizza shop, should consider what queries will be important for conducting business. Quite likely, Len will need to look up the price of a topping on a regular basis. A dictionary that maps toppings to prices would be ideal for this.

The instructions include the following, which was the motivation for counting the number of two-dollar toppings …

Your boss wants you to do some research on \$2 slices.

If Len anticipates the need to look up the types of toppings that are available for a given price on a regular basis, he could invert the toppings dictionary to maintain a mapping of each price to a list of toppings for that price.

Since the Len’s Slice project is all about lists, it does not prompt the user to create dictionaries, so perhaps using a dictionary is beyond the scope of the project. Still, it is worth thinking about how they could be used.

If we are to use lists rather than dictionaries, and use the `count` method …

Along those lines, you could do something like this …

``````num_two_dollar_slices = [2 for price, topping in pizzas if price == 2].count(2)
``````

… or, to generalize it …

``````def price_count(price):
return [price for p, t in pizzas if p == price].count(price)
num_two_dollar_slices = price_count(2)
``````

Edited on November 30, 2018 to provide the `price_count` function

Except we’d get the length, because they’re all equal to price (otherwise we’re asking count to do a similar filter again)

Or get the price out for each one and then count:

``````# A pizza is a price and a topping p, t
# Make a new list of only p:
[p for p, t in pizzas].count(2)
#                      and then count how many are equal to 2
``````

Or with the map function:

``````map(get_price, pizzas).count(2)
``````

(`get_price` isn’t defined, it would be a function that looks at a pizza and returns its price)

Hi, I am new on Python. Can you explain the 2 for price, topping in pizzas if price == 2 part? like what these arguments mean and how to use it? Thanks

That’s list comprehension.
The idea is that a loop is a box of values, so you can take out a value, decide what to do with it and whether or not to keep it. The list comprehension will then collect all such values into a list.
You can do the same thing with a loop.
As with everything else, google it.
It’ll probably seem alien to you at first but I think most people grow to quite like it.

Thanks, I kind of get it after Google list comprehension. Only the 2 on the very begging is not so sure. Is it just marking those items meet the conditions behind as a 2, so that the count() function could work on it?

The very first part of the list comprehension is the thing to keep for that list entry.
If you added 1 there, you would get threes instead of twos.

I just entered the loop section and I got it now lol, thanks.