In: Computer Science
how to check if there are keys with the same values in a dictionary. python 3.0
for example:
data = {'size': 4, 'entrance':(0, 0), 'exit':(0,0)}
since the entrance and exit keys have the same value, i want the function to return None.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
def is_having_same_values(data):
# Get all the values as follows
values = data.values()
print('All values are:', values)
unique_values = set(values)
print('Unique values are:', unique_values)
if len(values) != len(unique_values):
# Same values are exist
print("ALL keys are having same values")
return None
else:
# all are different
return "ALL keys are having different values"
# test1
print('============== TEST 1 ===========')
data = {'size': 4, 'entrance': (0, 0), 'exit': (0, 0)}
print(is_having_same_values(data))
# test2
print('============== TEST 2 ===========')
data = {'size': 4, 'entrance': (0, 1), 'exit': (0, 0)}
print(is_having_same_values(data))
====