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

前端 未结 6 1034
栀梦
栀梦 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条回答
  •  失恋的感觉
    2020-12-20 12:02

    No, Python dictionaries do not have inherent ordering, regardless of the key values. If you need ordering, stick to arrays or lists, or better yet - check out pandas, which will allow a similar ability to dictionaries to call by key value, as well as many other powerful features (http://pandas.pydata.org/pandas-docs/stable/10min.html).

提交回复
热议问题