0
checking if 2 dicts are equals
I have this program : def is_equal(obj_one, obj_two): return ( obj_one.keys()==obj_two.keys() and obj_one.values()==obj_two.values() ) obj_one = { "name": "Jason", "phone": "9853759720", "email": "jason@edabit.com" } obj_two = { "name": "Jason", "phone": "9853759720", "email": "jason@edabit.com" } print(is_equal(obj_one, obj_two)) I know that using obj_one.items()==obj_two.items() will give true result , but I can't get why using keys and values logic is giving false
5 odpowiedzi
+ 2
https://code.sololearn.com/cn67LdJLj854/?ref=app
Maybe the link in the code helps. I also don't know why it returns false, but it is documented.
0
mmm, I tried it using keys () alone, it gave true, so the problem is in the values () , still not clear why , though the values are equals
0
why not just use
obj_one==obj_two
? it compares keys and values. No need to break down the comparison manually to keys and values.
0
I guess it will give the results only for keys , but in order for dictionaries to be equals keys and values both should be the same in the 2 dicts
0
https://docs.python.org/3/library/stdtypes.html#dict.values
"An equality comparison between one dict.values() view and another will always return False. This also applies when comparing dict.values() to itself:"