I'm trying to optimize a nested for loops that compares an element in the array with the rest of the elements in the array.
There's two part, the first part is for example, an Array has 3 elements, and each element is a dictionary:
[{"someKey_1":"a"}, {"someKey_1":"b"}, {"somekey_1":"a"}]
1st iteration(1st element compares with 2nd element):
Test key of "someKey" for two elements, since a != b, then we do nothing
2st iteration(1st element compares with 3nd element):
Test key of "someKey" for two elements, since a == a, we do some logic
The code:
for idx, val in enumerate(set_of_pk_values):
for idx_2, val_2 in enumerate(set_of_pk_values):
if (val['someKey'] == val_2['someKey'] and idx != idx_2):
#Some Logic
The second part is very similar to the previous example (3 items in the list), in the same dictionary, we have an array associated with a key (now there's a single dictionary with two keys in each element of the array), let's say:
[{"someKey_1":[b,f]}{"someKey_2":a},
{"someKey_1":[e,f]}{"someKey_2":b},
{"somekey_1":[h,k]}{"someKey_2":c}]
1st iteration (1st element compares with 2nd element):
loops through the array with the key: someKey_1
b==b (2nd element's someKey_2), then do some logic
f!=b (2nd element's someKey_2), no logic is done
2nd iteration (1st element compares with 3rd element):
loops through the array with the key: someKey_1
b==c (3rd element's someKey_2), then do some logic
f!=c (3rd element's someKey_2), no logic is done
The code:
for idx, val in enumerate(set_of_pk_values):
for idx_2, val_2 in enumerate(set_of_pk_values):
for pred in val['someKey_1']:
if(val_2['someKey_2'] == pred):
#Some Logic
Currently the runtime for the first nested loop: 21 seconds, and the second nested loop is around 19 seconds. Compared to other processes, ranging from 1-2 seconds, this part is clearly a bottleneck.
Can anybody point me to the right direction on how to optimize this piece of simple, yet extremely time consuming code?