checking if 2 dicts are equals | Sololearn: Learn to code for FREE!


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": "[email protected]" } obj_two = { "name": "Jason", "phone": "9853759720", "email": "[email protected]" } 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/20/2022 6:30:48 PM

sum fatt

5 Answers

New Answer

+2 Maybe the link in the code helps. I also don't know why it returns false, but it is documented.


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


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.


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 "An equality comparison between one dict.values() view and another will always return False. This also applies when comparing dict.values() to itself:"