I’m trying to figure out a code that will go
through all items of the list, and keep a count of how many
times the given item occurs, without the count funtion!
This is my code:
shopping_cart= [
['Soap', 'Floss', 'Hand Sanitizer','Baby wipes'],
['Chickpeas', 'Blueberries', 'Granola'],
['Crayons','Construction paper','Printer ink']
]
count = 0
for [i] in shopping_cart:
count+=len(shopping_cart)
print (count)
>Solution :
I think the easiest way to do this sort of task is to just make a dictionary, add every item to it and then you can get the count for every item.
shopping_cart = [
['Soap', 'Floss', 'Hand Sanitizer','Baby wipes'],
['Chickpeas', 'Blueberries', 'Granola'],
['Crayons','Construction paper','Printer ink']
]
items = {}
for row in shopping_cart:
for item in row:
if item not in items:
items[item] = 0
items[item] += 1
print(items)