How to sort through a list of dicitonaries and get all orders with same restaurant name without defining the restaurant name?

Viewed 58

I have a list of dictionaries that make up restaurant orders. With this list, I want to loop through it and do something with all orders with matching restaurant name, and do this for each restaurant. Now, the big challenge with this is that this list is updated weekly, and there's new restaurants added. So I'm trying to figure out how to make a loop that gathers all orders with the same restaurant and groups them but without actually stating in the code the restaurant name. Meaning I believe the logic should be:

for rest in order_list:
    for allorders_of_restaurant1_found:
        ...
    for allorders_of_restaurant2_found:
        ... 

A sample of the list:

[{'order': '23327', 'restaurant': 'Big Bar & Grill', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23329', 'restaurant': 'Big Bar & Grill', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23325', 'restaurant': 'Boneyard', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23332', 'restaurant': 'Boneyard', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23321', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23322', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23323', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}]

How can I parse the list and sort orders by restaurant without having to use, for example, if rest == "Maggios"? What would be the best way to do this?

2 Answers

You should create a new mapping where orders are grouped together by restaurant names. {Restaurant_Name: [order1, order2,...]}.

You can make use of defaultdict for that.
Please see below

>>> orders
[{'order': '23327', 'restaurant': 'Big Bar & Grill', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23329', 'restaurant': 'Big Bar & Grill', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23325', 'restaurant': 'Boneyard', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23332', 'restaurant': 'Boneyard', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23321', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23322', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23323', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}]
>>> 
>>> from collections import defaultdict
>>> orders_per_restaurant = defaultdict(list)
>>> 
>>> for order in orders:
...     orders_per_restaurant[order.get('restaurant', "RESTAURANT_NOT_FOUND")].append(order)
... 
>>> orders_per_restaurant
defaultdict(<class 'list'>, {'Big Bar & Grill': [{'order': '23327', 'restaurant': 'Big Bar & Grill', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23329', 'restaurant': 'Big Bar & Grill', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}], 'Boneyard': [{'order': '23325', 'restaurant': 'Boneyard', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23332', 'restaurant': 'Boneyard', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}], 'Maggios': [{'order': '23321', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23322', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}, {'order': '23323', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}]})
>>> 
>>> orders_per_restaurant.keys()
dict_keys(['Big Bar & Grill', 'Boneyard', 'Maggios'])
>>> 

Assuming you then want to process them, you can use items() or keys() as per your convenience. See this link also.

>>> for restaurant_name, restaurant_orders in orders_per_restaurant.items():
...     for order in restaurant_orders:
...             print(order)
...     print(f'Above orders are for {restaurant_name}')
... 
{'order': '23327', 'restaurant': 'Big Bar & Grill', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}
{'order': '23329', 'restaurant': 'Big Bar & Grill', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}
Above orders are for Big Bar & Grill
{'order': '23325', 'restaurant': 'Boneyard', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}
{'order': '23332', 'restaurant': 'Boneyard', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}
Above orders are for Boneyard
{'order': '23321', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}
{'order': '23322', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}
{'order': '23323', 'restaurant': 'Maggios', 'sub-total': '131.43', 'date': 'Mar 28, 2021'}
Above orders are for Maggios
>>> 

May consider using another dictionary to achieve it. For example:

# dictionary for each restaurant orders
restaurantOrderDict = dict()

# loop through the order list
for order in order_list:
    if order.get('restaurant') in restaurantOrderDict:
        restaurantOrderDict[order.get('restaurant')].append(order)
    else:
        restaurantOrderDict[order.get('restaurant')] = []
        restaurantOrderDict[order.get('restaurant')].append(order)
Related