Ex 39 abbrev not mentioned earlier

#print every state abbreviation
print("-" * 10)
for state, abbrev in list(states.items()):
print(f"{state} is abbreviated {abbrev}")

#print every state abbreviation
print("-" * 10)
for abbrev, city in list(cities.items()):
print(f"{states} has the city {city}")

#now do both at the same time
print("-" * 10)
for state, abbrev in list(states.items()):
print(f"{state} state is abbreviated {abbrev}")
print(f"and has city {city}")

THIS PART OF THE CODE, WHERE ABBREV IS NOT MENTIONED EARLIER AND USED HERE,I AM NOT ABLE TO UNDERSTAND.

Hello @aumo and welcome to the forum.

”abbrev” is just a shortening for abbreviation.

And its just a variable name in this code.
You can use another name if you want.

1 Like

so in this code if i am not wrong the for line in which abbrev was used the first time was:
abbrev = states.items()
and items is a func to access the items in the dict data structure
have i understood it correctly?
thankyou for the reply @ulfen69

Close but not quite I think.

print(abbrev.items())

would give you all of the contents in the dict like this:

dict_items([(‘Oregon’, ‘OR’), (‘Florida’, ‘FL’)])

But when two variables are put into a for loop like that they got the key and the value respectively

So for each turn in the loop they will get:

state = states.keys() # will get the name of the state
abbrev = states.values() # will get the abbreviations for the state.

Then everything is printed out for you.

Yes, that’ right, but it was a mistake on my part to do list(abbrev.items()) you can drop the list() part and it still works.

A free service run by Zed A. Shaw for learncodethehardway.org.