Home > Net >  How to get specific objects from two list of dictionaries on a specific key value?
How to get specific objects from two list of dictionaries on a specific key value?

Time:11-30

I have two lists of dictionaries:

timing = [
    {"day_name": "sunday"},
    {"day_name": "monday"},
    {"day_name": "tuesday"},
    {"day_name": "wednesday"},
    {"day_name": "thursday"},
    {"day_name": "friday"},
    {"day_name": "saturday"},
]

hours_detail = [
    {"day_name": "sunday", "peak_hour": False},
    {"day_name": "monday", "peak_hour": False},
    {"day_name": "tuesday", "peak_hour": False},
    {"day_name": "wednesday", "peak_hour": False},
    {"day_name": "thursday", "peak_hour": False},
    {"day_name": "friday", "peak_hour": False},
    {"day_name": "saturday", "peak_hour": False},

    {"day_name": "saturday", "peak_hour": True},
    {"day_name": "friday", "peak_hour": True},
    {"day_name": "thursday", "peak_hour": True},
]

I want to create another list of dictionaries that looks like the one below. I'm basically combining these two lists and also rearranging according to the day name.

final_data_object = [
    {
        "timing": {"day_name": "saturday"},
        "hour_detail": [
            {"day_name": "saturday", "peak_hour": False},
            {"day_name": "saturday", "peak_hour": True},
        ]
    },
    {
        "timing": {"day_name": "friday"},
        "hour_detail": [
            {"day_name": "friday", "peak_hour": False},
            {"day_name": "friday", "peak_hour": True},
        ]
    },
    soon on...
]

I have tried this but it didn't work:

data = []
for time_instance in timing:
    obj = {
        "timing": time_instance
    }
    for hour_instance in hour_detail:
        if time_instance["day_name"] == hour_instance["day_name"]:
            obj["hour_detail"] = hour_instance
            data.append(obj)

return data

CodePudding user response:

If pricing = hour_detail then obj["pricing"] must be a list as it can have multiple value.

You have to create a new list in your loop for time_instance in timing: and append every time time_instance["day_name"] == pricing_instance["day_name"].

Exemple:

data = []

for time_instance  in timing:
    current_hour_detail = []
    for line in hours_detail:
        if line["day_name"] == time_instance["day_name"]:
            current_hour_detail.append(line)
            
    data.append({
        "timing": time_instance,
        "pricing": current_hour_detail
    })

Or smaller way

data = []

for time_instance  in timing:
    current_hour_detail = [line for line in hours_detail if line["day_name"] == time_instance["day_name"]]
    data.append({
        "timing": time_instance,
        "pricing": current_hour_detail
    })

Whatch out tho, this solution is in O(n*m) time complexity, by first transforming your list hours_detail by a hashtable {day_name: [peak_hour1, peak_hour2]} you can reduce it in O(n m)

CodePudding user response:

This should answer you question:

...
data = []
for time_instance in timing:
    obj = {"timing": time_instance}
    # use a list to store all your pricing
    obj["hour_detail"] = []
    for pricing_instance in pricing:
        if time_instance["day_name"] == pricing_instance["day_name"]:
            obj["hour_detail"].append(pricing_instance)
    # add 'obj' after the loop
    data.append(obj)
print(data)
  • Related