I can not figure out the order of dictionary keys, they seemed not to be sorted. Any insight?
bumsfeld
413
Nearly a Posting Virtuoso
Recommended Answers
Jump to PostAn example would be nice, one that surprises you by the outcome.
Jump to PostFrom the Dive Into Python tutorial:
"Dictionaries have no concept of order among elements. It is incorrect to say that the elements are 'out of order'; they are simply unordered. This is an important distinction which will annoy you when you want to access the elements …
Jump to PostThis little test using your example will show you that dictionary keys do have a certain order ...
# these all give the same result = {'a': 1, 'c': 3, 'b': 2, 'd': 4} # so there must be a reason why keys are in a certain …
Jump to PostHi!
Well, a dictionary (some call it hash) is per definition unordered. If you need it in some specific order, use another data structure, or just sort it ;) e.g.
d = {'a': 1, 'c': 3, 'b': 2} for key in sorted(d.keys()): print "%s => %s" …
All 13 Replies
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
G-Do
19
Junior Poster
bumsfeld
413
Nearly a Posting Virtuoso
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
bumsfeld
413
Nearly a Posting Virtuoso
mawe
6
Junior Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
bumsfeld
413
Nearly a Posting Virtuoso
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
bumsfeld
413
Nearly a Posting Virtuoso
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
bumsfeld
413
Nearly a Posting Virtuoso
G-Do
19
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.