Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary Comparison/Conditional check in Python

I have two list of dictionaries in the format:

list1 = [
    {"time": "2024-01-29T18:32:24.000Z", "value": "abc"},
    {"time": "2024-01-30T19:47:48.000Z", "value": "def"},
    {"time": "2024-01-30T19:24:20.000Z", "value": "ghi"}
]

list2 = [
    {"time": "2024-01-30T18:34:44.000Z", "value": "xyz"},
    {"time": "2024-01-30T19:47:48.000Z", "value": "pqr"},
    {"time": "2024-01-30T19:24:20.000Z", "value": "jkl"}
]

Requirement : I need to compare value of "time" key of each dictionary in list1 with "time" key of each dictionary in list2 and if they are equal, will need to form a key value pair dictionary for subsequent lookups.
In the above, list1 and list2 have 2 dictionaries with same time :
"2024-01-30T19:47:48.000Z"
"2024-01-30T19:24:20.000Z"
I need to combine value of these 2 to form output as below :

Output :

{"def" : "pqr", "ghi" : "jkl"}

using itertools.product function , i am able to list as below so far but not in the key value pair dictionary as required .

import itertools

list1 = [{"time": "2024-01-29T18:32:24.000Z", "value": "abc"}, {"time": "2024-01-30T19:47:48.000Z", "value": "def"}, {"time": "2024-01-30T19:24:20.000Z", "value": "ghi"}]
list2 = [{"time": "2024-01-30T18:34:44.000Z", "value": "xyz"}, {"time": "2024-01-30T19:47:48.000Z", "value": "pqr"}, {"time": "2024-01-30T19:24:20.000Z", "value": "jkl"}]

output = [f"{x['value']} : {y['value']}"  if x['time'] == y['time'] else None for (x, y) in itertools.product(list1, list2)]
print(output)

Current Output :

[None, None, None, None, 'def : pqr', None, None, None, 'ghi : jkl']

I'm wondering if i can use lambda function to achieve output as : {"def" : "pqr", "ghi" : "jkl"}
Any help or suggestions is appreciated.

like image 739
pats4u Avatar asked Aug 30 '26 14:08

pats4u


1 Answers

I'd create temporary index to ease the search:

list1 = [
    {"time": "2024-01-29T18:32:24.000Z", "value": "abc"},
    {"time": "2024-01-30T19:47:48.000Z", "value": "def"},
    {"time": "2024-01-30T19:24:20.000Z", "value": "ghi"},
]

list2 = [
    {"time": "2024-01-30T18:34:44.000Z", "value": "xyz"},
    {"time": "2024-01-30T19:47:48.000Z", "value": "pqr"},
    {"time": "2024-01-30T19:24:20.000Z", "value": "jkl"},
]

tmp = {d["time"]: d for d in list1}

out = {}
for d in list2:
    if d["time"] in tmp:
        out[tmp[d["time"]]["value"]] = d["value"]

print(out)

Prints:

{'def': 'pqr', 'ghi': 'jkl'}
like image 161
Andrej Kesely Avatar answered Sep 01 '26 04:09

Andrej Kesely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!