I have two dictionaries that I’d liked to compare their dict_values based on certain criteria, say the value at index 0 and 2 on their respective dict_values have to be the same to be considered equal.
This code works:
a = dict()
b = dict()
a[1,3] = 5, 6, 7
b[1,4] = 5, 9, 7
a_tuple = (list(a.values()))[0]
b_tuple = (list(b.values()))[0]
if a_tuple[0] == b_tuple[0] and a_tuple[2] == b_tuple[2]:
print('Equal')
Is there a shorter/efficient way to do this ?.
Thanks in advance!
John
I’m not sure if this is much better, but the following should work:
The comparison is a little more clean and I use
nextanditerto get the first tuple in the dictionary if using python 3.Of course, the better solution would be change the data structure so you’re not constantly dealing with dictionaries with only 1 item.