Pythonic way to check if two dictionaries have the identical set of keys?

前端 未结 6 2066
日久生厌
日久生厌 2020-12-14 15:50

For example, let\'s say I have to dictionaries:

d_1 = {\'peter\': 1, \'adam\': 2, \'david\': 3}

and

d_2 = {\'peter\': 14, \         


        
6条回答
  •  死守一世寂寞
    2020-12-14 16:23

    In Python2,

    set(d_1) == set(d_2)
    

    In Python3, you can do this which may be a tiny bit more efficient than creating sets

    d1.keys() == d2.keys()
    

    although the Python2 way would work too

提交回复
热议问题