In Python, how do I iterate over a dictionary in sorted key order?

后端 未结 10 984
有刺的猬
有刺的猬 2020-11-27 10:19

There\'s an existing function that ends in the following, where d is a dictionary:

return d.iteritems()

that returns an unsort

10条回答
  •  春和景丽
    2020-11-27 10:39

    Greg's answer is right. Note that in Python 3.0 you'll have to do

    sorted(dict.items())
    

    as iteritems will be gone.

提交回复
热议问题