0

I'm trying to figure out why this code:

states = {
    'Oregon': 'OR',
    'Florida': 'FL',
    'California': 'CA',
    'New York': 'NY',
    'Michigan': 'MI'
}

print '-' * 10
for state, abbrev in states.items():
    print "%s is abbreviated %s" % (state, abbrev)

which gives this output:

----------
California is abbreviated CA
Michigan is abbreviated MI
New York is abbreviated NY
Florida is abbreviated FL
Oregon is abbreviated OR

is coming out in this particular order? Shouldn't the output be in the order that's in the dictionary?

ie. I would think that it'd come out like:


Oregon is abbreviated OR
Florida is abbreviated FL
California is abbreviated CA
New York is abbreviated NY
Michigan is abbreviated MI

1 Answers1

0

Dictionaries do not have inherent order. Use collections.OrderedDict if you care about the ordering of items in a dict.

a p
  • 3,098
  • 2
  • 24
  • 46