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

前端 未结 6 2055
日久生厌
日久生厌 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:15

    One way is to check for symmetric difference (new set with elements in either s or t but not both):

    set(d_1.keys()).symmetric_difference(set(d_2.keys()))
    

    But a shorter way it to just compare the sets:

    set(d_1) == set(d_2)
    

提交回复
热议问题