iterate over dict to find identical values in python

Viewed 46

let's assume I have x = (600, 800 , 700) and I have also the following dict:

enter image description here

I want to find the equivalent values for x from the dict (which is highlighted in the picture) knowing that the values in dict might be swapped in an unknown order.

Is that possible without writing complex searching code?

3 Answers

if we want just to know if there is a match for numbers in values_to_find and in dict_to search, we can use all(...), and no sorting is required:

value_to_find = (600, 800, 700)

dict_to_search = {
    'key_01':(40, 300, 300),
    'key_02':(800, 500, 1000),
    'key_03':(500, 1000, 200),
    'key_04':(800, 700, 600)
}

for k, v in dict_to_search.items():
    if all([i in value_to_find for i in v]):
        print(k)

the result is: 'key_04'

You could sort the values you're looking for and sort each entry in the dictionary before the comparison:

value_to_find = (600, 800, 700)

dict_to_search = {
    'key_01':(40, 300, 300),
    'key_02':(800, 500, 1000),
    'key_03':(500, 1000, 200),
    'key_04':(800, 700, 600)
}

value_to_find = sorted(value_to_find)
for key in dict_to_search:
    sorted_value = sorted(dict_to_search[key])
    if sorted_value == value_to_find:
        print(f'matching values found against {key}: {dict_to_search[key]}')
        break

Output:

matching values found against key_04: (800, 700, 600)

Since the order of the numbers is irrelevant, you can convert the tuples to sets (which are unordered) and then simply check for equality.

value_to_find = (600, 800, 700)

dict_to_search = {
    'key_01':(40, 300, 300),
    'key_02':(800, 500, 1000),
    'key_03':(500, 1000, 200),
    'key_04':(800, 700, 600)
}

result = []
for key,value in dict_to_search.items():
    if set(value) == set(value_to_find):
        result.append(key)

print(result)

This outputs ['key_04']

You could also use list comprehension to do it in one line

result = [key for key,value in dict_to_search.items() if set(value)==set(value_to_find)]
Related