0

I am following Zed Shaw's "Learn Python The Hard Way, 3rd Edition"

Here is a small code sample from exercise 39 of the book:

states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI' }
for state, abbrev in states.items():
    print "%s is abbreviated %s" % (state, abbrev)
print "-" * 10
for abbrev, state in states.items():
    print "%s is abbreviated %s" % (abbrev, state)

The output of the above program is:

C:\>python code.py
California is abbreviated CA
Michigan is abbreviated MI
New York is abbreviated NY
Florida is abbreviated FL
Oregon is abbreviated OR
----------
California is abbreviated CA
Michigan is abbreviated MI
New York is abbreviated NY
Florida is abbreviated FL
Oregon is abbreviated OR
C:\>

In the code, no where we are telling python what are states and what are abbreviations. My question is: How come both print statements print the same? How does python display the appropriate "state" and "abreviation" even if I reverse the order in the second for loop?

How should i write the for loop if I want to show the abbreviation (value) first and then the (key).

Note: Thank you for your answer. Stackoverflow told me that in order to add anything to my question I should edit the question instead of adding a new comment....

1
  • You reversed it in two places, and those changes cancel each other out. Note that Python has no idea what the names state or abbrev mean; you would have seen the same effect if you'd named those variables foo and bar. Commented Sep 3, 2014 at 3:54

5 Answers 5

1

My question is: How come both print statements print the same? How does python display the appropriate "state" and "abbreviation" even if I reverse the order in the second for loop?

If you change the variable names to a,b and then b,a it would do the same thing:

for a,b in states.items():
   print "%s is abbreviated %s" % (a,b)


for b,a in states.items():
   print "%s is abbreviated %s" % (b,a)

.items() takes each key value pair and returns it as a tuple. a,b is called tuple unpacking. It takes each member of the tuple, and assigns it to a separate variable.

So it really doesn't matter what you call the variables, because .items() will always return a key,value pair, and it will always be in that order.

To write the abbreviation first, just switch the order of the variables in the print statement:

for a,b in states.items():
    print '%s is the abbreviation for %s' % (b,a)
Sign up to request clarification or add additional context in comments.

Comments

0

Dictionaries are not symmetric; there is a key and there is a value and the roles are not reversible.

The function items() returns a collection of tuples in which the first element is the key and the second is the value. In the second loop, you reversed the names you gave the key and the value, but the names of the variables don't matter outside the program.

Comments

0

In the first one you are saying state, abbrev and then you are calling print "%s is abbreviated %s" % (state, abbrev). When you say state you are saying that is the key and then abbrev is the value (of the key value pairs of the dictionary). Then when you call the print line it is having the key first and the value after.

In the second section you are just switching the order of state and abbrev. The program doesn't know if state means the full name of the state or the abbreviation (and vice versa) just that one is the key and the other is the value.

So to recap:

dictionary = {
key : value
}

In the first section the key is state and value is abbrev. In the second section it is reversed.

Comments

0

The list is the same but the difference is only in the key, value name.

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

In the first for loop you have key = state and value = abbrev. In the second loop key = abbrev and value = state.

Comments

0

Take a look at the order of the variables in the for loops and how they are reversed in the print statements. In one loop it's settings the state name (e.g. California) to the variable 'state'

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

In the second loop it is merely reversing the variables:

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

so now 'abbrev' refers to "California". Because they are printed in the same order they are set, the results are the same. I think it is merely showing you how the assignments work on the for loops.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.