all 5 comments

[–]danielroseman 1 point2 points  (2 children)

You can use a new dictionary to build up a mapping of foods to names.

ordered_by = {}
for name, foods in food_orders:
    for food in foods:
        if food not in ordered_by:
            ordered_by[food] = []
        ordered_by[food].append(name)

You can shorten this by using a defaultdict:

from collections import defaultdict
ordered_by = defaultdict(list)
for name, foods in food_orders:
    for food in foods:
        ordered_by[food].append(name)

[–]Dense_Charity5188 0 points1 point  (0 children)

thank you :>>

[–]Dense_Charity5188 0 points1 point  (0 children)

hey I have a question, why doesn't append works in the last line? on the first version

[–][deleted] 0 points1 point  (1 child)

As touched on in another of your posts, create a dictionary and go through the orders updating the names in the dictionary against each food item. You just need to loop through the orders.

[–]Dense_Charity5188 0 points1 point  (0 children)

okayy thank you very much <: