Len's Slice - Why can I use pizzas.count(2) to find the number of 2s in that list?

For counting the number of two dollar slices, why can’t I use pizzas.count(2) to find the number of 2s in that list?

toppings = ['pepperoni', 'pineapple', 'cheese', 'sausage', 'olives', 'anchovies', 'mushrooms']

prices = [2, 6, 1, 3, 2, 7, 2]

num_pizzas = len(toppings)

pizzas = list(zip(prices, toppings))

pizzas.sort()

cheapest_pizza = pizzas[0]

priciest_pizza = pizzas[-1]

three_cheapest = pizzas[:3]

num_two_dollar_slices = prices.count(2)

print("We sell %s different kinds of pizza!"

  % (num_pizzas)

)

print(pizzas)

print(three_cheapest)

print(num_two_dollar_slices)

Hi, Welcome to the forum! Use the </> button before pasting your code so that it’s formatted properly, it really counts with helping users help you!

You can use count(2) but the problem is what if they’re another item that’s going to also be 2 dollars? It would be a very highly contextual use of the count method… (meaning you can’t apply it or scale it in a general sense).

1 Like

For some reason when I run this (with pizzas.count(2)) I end up with 0?

You’d have to figure out what list your counting from. Do you mean to count from the prices list? Have you printed the pizzas list to see if there are any 2’s in it?

Because pizzas is a list with tuples (I think) inside of it. There isn’t just a 2 inside of it. There is

(2, 'topping') 

It’s not possible to use count. Now, you could do something like this:

num_two_dollar_slices = len([item[0] for item in pizzas if item[0] == 2])
print(num_two_dollar_slices)#3

For some reason when I run this (with pizzas.count(2)) I end up with 0?

Since there isn’t just a 2 in pizzas, it returns 0.

4 Likes

I have encountered the same problem. I even got frustrated and watched the youtube tutorial. Her code returns 3 while mine returns 0, but our codes are identical.
the hint for this step: You can use .count to find the number of occurrences of a value in a list:

my_list = ["a", "a", "b"] number_of_as = my_list.count("a")# number_of_as is 2

the tutorial: Lens Slice - YouTube

my code:

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

I have actually done this assignment before the course was updated (and I restarted it instead of just doing the new material…like a fool) and I don’t recall having this problem then. Maybe the original material was incorrect (due to the tuples explanation) and was corrected, but the hint and tutorial were accidentally overlooked. I am just speculating. I am a beginner but you can see that the hint directs us to write it this way and so does the walkthrough. Good luck! :slight_smile:

The reply above this covered this- Len's Slice - Why can I use pizzas.count(2) to find the number of 2s in that list? - #5 by h1lo but if that’s not clear to you then please read on-

Consider the first element of your pizzas list, since you sorted and assigned it to cheapest_pizza you could just print it.

print(cheapest_pizza)
Out: (1, 'cheese')

Can you see how the .count() method might struggle with an element like this? In order to get a count of 1 you’d need to use pizzas.count((1, 'cheese')).

Yes, that makes sense. Thank you. :slight_smile: I was just letting the original poster know that I understand where their confusion comes from and that the provided hint and video also seem to be incorrect. I might be mistaken. But if you look at the code in the video, it seems to make the same mistake but gives the correct response.

I believe the video uses the method of prices rather than pizzas (it’s clearer on a big screen) so .count() would work on the elements of that list which isn’t populated by tuples.

1 Like

Thank you! I actually did not realize that.