Count a zipped list in python


#1

On Len’s Slice
https://www.codecademy.com/paths/data-science/tracks/dspath-python-lists-and-loops/modules/dspath-lists/projects/lens-slice

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[0]
priciest_pizza = pizzas[-1]
three_cheapest = pizzas[:3]
print(three_cheapest)
num_two_dollar_slices = prices.count(2)
print(num_two_dollar_slices)


#2

Hi @bitplayer48198,

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


#3

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


#4

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


#5

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)