How to add items from a list as values to corresponding dictionary key? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

How to add items from a list as values to corresponding dictionary key?

I want to sort groceries from an shopping list into categories using a dictionary. I have created the following 4 lists: DAIRY= ["milk", "cheese", "butter"] MEAT= ["steak", "ham", "sausage"] VEG= ["broccoli", "tomato", "carrot", "eggplant"] FRUIT = ["apples", "bananas", "oranges"] and the following dictionary : Shopping_List = {"VEG": [], "FRUIT": [], "MEAT": [], "DAIRY": [], "OTHER": []} When a word from the input list matches an element on one of the 4 lists, I want it to be added as a value to the corresponding key in the dictionary, e.g if the input is "bananas",which is an element of the list FRUIT, I want it to be added as a value for the dictionary key FRUIT {"FRUIT": ["bananas"]}. Furthermore, If the input word isn't on any of the lists,f it should be added as a value for the dictionary key OTHER. My goal is to create a function that does the following: With an input list,such as, this : input_list: ["bananas", "broccoli","carrot", "egg", "steak", "milk", "cookies", "water"] the function groceries (input_list) should return the following dictionary: {"VEG": ["carrot", "broccoli"], "FRUIT": ["bananas"], "MEAT": ["steak"], "DAIRY": ["milk"], "OTHER": ["cookies", "eggs", "water"]}. I am currently stuck as I don't know how to add the items from the input list to the empty value list of the matching dictionary key. I would really appreciate it if someone could give me some pointers.

27th Jun 2020, 5:25 PM
PhoenixCoding
6 ответов
+ 4
dict["VEG"] =[it for it in VEG if it in input_list]
27th Jun 2020, 5:51 PM
Oma Falk
Oma Falk - avatar
27th Jun 2020, 7:15 PM
Oma Falk
Oma Falk - avatar
+ 1
DAIRY= ["milk", "cheese", "butter"] MEAT= ["steak", "ham", "sausage"] VEG= ["broccoli", "tomato", "carrot", "eggplant"] FRUIT = ["apples", "bananas", "oranges"] input_list = ["bananas", "broccoli","carrot", "egg", "steak", "milk", "cookies", "water"] d = {x : [] for x in ['FRUIT','DAIRY','MEAT', 'VEG']} for item in input_list: if item in FRUIT: d['FRUIT'].append(item) if item in DAIRY: d['DAIRY' ].append(item) if item in VEG: d['VEG' ].append(item) if item in MEAT: d['MEAT' ].append(item) print(d)
27th Jun 2020, 5:49 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
DAIRY= ["milk", "cheese", "butter"] MEAT= ["steak", "ham", "sausage"] VEG= ["broccoli", "tomato", "carrot", "eggplant"] FRUIT = ["apples", "bananas", "oranges"] input_list = ["bananas", "broccoli","carrot", "egg", "steak", "milk", "cookies", "water"] d = {x : [] for x in ['FRUIT','DAIRY','MEAT', 'VEG']} for item in input_list: for name in ['FRUIT','DAIRY','MEAT', 'VEG']: if item in globals()[name]: d[name].append(item) print(d)
27th Jun 2020, 6:00 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
If you store categories in the dictonary instead of separate lists you get flexibility without any tricks such as searching variables names in globals. https://code.sololearn.com/c3lCTpWCdBxJ/?ref=app
27th Jun 2020, 9:40 PM
portpass
0
Extending by adding product price. But one problem, for uncategorized products I had to use a generic price. https://code.sololearn.com/cjHBj0466qTJ/?ref=app
28th Jun 2020, 3:21 PM
Ipang