checking if 2 dicts are equals | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

20th May 2022, 6:30 PM
sum fatt
5 Answers
+ 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.
20th May 2022, 8:01 PM
Fu Foy
Fu Foy - avatar
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
20th May 2022, 8:46 PM
sum fatt
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.
20th May 2022, 9:26 PM
Bob_Li
Bob_Li - avatar
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
20th May 2022, 9:29 PM
sum fatt
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:"
21st May 2022, 7:11 AM
Fu Foy
Fu Foy - avatar