Order in the dict

Python doesn't store dictionary elements in any particular order. If you enter elements in one order, they may be stored in another (essentially random) order inside Python. Here's an example:

>>> lumberjack = {'sleep': "All night", 'work': "All day"} >>> lumberjack

The steps to make a sorted list of dictionary keys depend on your Python version. Python 2.4 and later

If you have Python 2.4 or later, follow these steps to get a sorted (usually alphanumeric) list of the keys of a dictionary:

Was this article helpful?

0 0

Post a comment