Im new to programming, and im designing a app where people can place orders, and i want to make it so that when someone makes the same order twice, the order would be added to the same list and its price value multiplied by the amount of times its repeated, then pop the old repeated keys, how do i go about this?
Dictionary = {item: 1, item2: 2, item3: 3}
List = [item, item3, item3]
For item in list:
If item in list (idk what to put here)
Final_amount.append(Dictionary[price])
Final_amount.pop(item3)
Print(list)
Output would be: 1, 6
CodePudding user response:
A counter will give you the counts by item, which you can then use to multiply by price.
from collections import Counter
d = {'item': 1, 'item2': 2, 'item3': 3}
l = ['item', 'item3', 'item3']
c = Counter(l)
for k in c.keys():
c[k]=c[k]*d.get(k)
print(c)
Output
Counter({'item3': 6, 'item': 1})
CodePudding user response:
I think Counter is the thing you are looking for. Here is a snippet of how it works:
from collections import Counter
my_list = ['1', '2', '3', '1', '2', '2']
new_dict = Counter(my_list)
print(new_dict['1'])
# output is 2