Python dictionary: are keys() and values() always the same order?

后端 未结 8 1585
难免孤独
难免孤独 2020-11-22 12:18

It looks like the lists returned by keys() and values() methods of a dictionary are always a 1-to-1 mapping (assuming the dictionary is not altered

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 12:44

    Yes it is guaranteed in python 2.x:

    If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.

提交回复
热议问题