Will a Python dict with integers as keys be naturally sorted?

前端 未结 6 1037
栀梦
栀梦 2020-12-20 11:39

If I create a Python dict which uses integers as keys, can I safely assume that iterating over the dict will retrieve items in order according to key value?

i.e. wil

6条回答
  •  旧时难觅i
    2020-12-20 12:28

    I don't think so. You have to make use of collections.OrderedDict in order to ensure ordering. However, this will sort the entries in the order they were added.

提交回复
热议问题