why elements of python dictionary not get printed in sequence
Question:
i have this dictionary
sr = {'DTW':'CNT','FWA':'CNT','GRR':'CNT','IND':'CNT','MKE':'CNT','MLI':'CNT','MSP':'CNT','OMA':'CNT','ORD':'CNT','SBN':'CNT','STL':'CNT','BDL':'EST','BOS':'EST','BWI':'EST','CLE':'EST','CMH':'EST','CVG':'EST','EWR':'EST','IAD':'EST','JFK':'EST','MDT':'EST','PHL':'EST','PIT':'EST','ROC':'EST','ABQ':'CNT','AUS':'CNT','DEN':'CNT','DFW':'CNT','ELP':'CNT','IAH':'CNT','LRD':'CNT','MCI':'CNT','MFE':'CNT','MSY':'CNT','OKC':'CNT','SAT':'CNT','TUL':'CNT','ATL':'EST','BNA':'EST','CLT':'EST','JAX':'EST','MCO':'EST','MEM':'EST','MIA':'EST','RDU':'EST','RIC':'EST','SDF':'EST','SJU':'EST','TPA':'EST','ANC':'WST','HNL':'WST','LAS':'WST','LAX':'WST','PDX':'WST','PHX':'WST','RNO':'WST','SAN':'WST','SEA':'WST','SFO':'WST','SLC':'WST','SMF':'WST','TUS':'WST'}
for s, r in sr.iteritems():
print s, r
but while printing it is printing starting with
JFK EST
MKE CNT
why? why printing is not staring with DTW?
Answers:
Because the implementation of a dict is a hashmap or hash table, wich doesn’t store the elements in order.
From the docs
CPython implementation detail: Keys
and values are listed in an arbitrary
order which is non-random, varies
across Python implementations, and
depends on the dictionary’s history of
insertions and deletions.
If the order is important, you should use the OrderedDict
PyMOTO OrderedDict tutorial provides good examples about OrderedDict().
Note that this has changed since Python 3.7
From https://docs.python.org/3/library/stdtypes.html#mapping-types-dict – "Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6."
i have this dictionary
sr = {'DTW':'CNT','FWA':'CNT','GRR':'CNT','IND':'CNT','MKE':'CNT','MLI':'CNT','MSP':'CNT','OMA':'CNT','ORD':'CNT','SBN':'CNT','STL':'CNT','BDL':'EST','BOS':'EST','BWI':'EST','CLE':'EST','CMH':'EST','CVG':'EST','EWR':'EST','IAD':'EST','JFK':'EST','MDT':'EST','PHL':'EST','PIT':'EST','ROC':'EST','ABQ':'CNT','AUS':'CNT','DEN':'CNT','DFW':'CNT','ELP':'CNT','IAH':'CNT','LRD':'CNT','MCI':'CNT','MFE':'CNT','MSY':'CNT','OKC':'CNT','SAT':'CNT','TUL':'CNT','ATL':'EST','BNA':'EST','CLT':'EST','JAX':'EST','MCO':'EST','MEM':'EST','MIA':'EST','RDU':'EST','RIC':'EST','SDF':'EST','SJU':'EST','TPA':'EST','ANC':'WST','HNL':'WST','LAS':'WST','LAX':'WST','PDX':'WST','PHX':'WST','RNO':'WST','SAN':'WST','SEA':'WST','SFO':'WST','SLC':'WST','SMF':'WST','TUS':'WST'}
for s, r in sr.iteritems():
print s, r
but while printing it is printing starting with
JFK EST
MKE CNT
why? why printing is not staring with DTW?
Because the implementation of a dict is a hashmap or hash table, wich doesn’t store the elements in order.
From the docs
CPython implementation detail: Keys
and values are listed in an arbitrary
order which is non-random, varies
across Python implementations, and
depends on the dictionary’s history of
insertions and deletions.
If the order is important, you should use the OrderedDict
PyMOTO OrderedDict tutorial provides good examples about OrderedDict().
Note that this has changed since Python 3.7
From https://docs.python.org/3/library/stdtypes.html#mapping-types-dict – "Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6."