I have 2 Dictionaries,
data1 = {
"key": [
{
"id": "key1",
"name": "key1"
},
{
"id": "key2",
"name": "key2"
},
{
"id": "key3",
"name": "key3"
},
]
}
data2 = {
"key": [
{
"id": "key2"
"name": "TEST key2"
},
{
"id": "key1",
"name": "TEST key1"
},
]
}
I am making a list of tuples of the objects inside the key
list in data1
and data2
having matching id
, using the below code
common_keys = [
(each_data1_key, each_data2_key)
for each_data1_key in data1.get("key", [])
for each_data2_key in data2.get("key", [])
if each_data1_key.get("id") == each_data2_key.get("id")
]
# Example result = [({"id":"key1", "name": "key1"}, {"id": "key1", "name": "TEST key1"}), ...]
Now I want to use these tuples for processing further in threadPoolExecutor's map function. Currently, I am using below code,
def func(object1, object2):
"""
func is being run in the thread to do some task parallelly with object1 and object2
"""
<SOME CODE HERE> ...
def myfunc(common_keys):
if common_keys:
with ThreadPoolExecutor(max_workers=10) as executor:
executor.map(lambda x: func(*x), common_keys)
# func is a function that accepts 2 objects as parameters
# since we are sending tuple of the object in threads, in order to process some task
My task is to optimize the code by reducing the loop(I have used nested for loop to find common_keys
list`
Could anyone help me to find any solution in which, in order to get a list of tuples of the objects having the same id, I don't need to use a nested loop(or, with another optimized way)?