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

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

    You can get the keys for a dictionary with dict.keys().

    You can turn this into a set with set(dict.keys())

    You can compare sets with ==

    To sum up:

    set(d_1.keys()) == set(d_2.keys())
    

    will give you what you want.

提交回复
热议问题