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

后端 未结 8 1665
难免孤独
难免孤独 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:30

    Yes, what you observed is indeed a guaranteed property -- keys(), values() and items() return lists in congruent order if the dict is not altered. iterkeys() &c also iterate in the same order as the corresponding lists.

提交回复
热议问题