I’m in exercise 39 and I’m a bit confused what lists exactly does in this bit of code:
#create a mapping of state to abbreviation
states = {
'Oregon':'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
"Michigan": 'MI'
}
# creates a basic set of states and some cities in them
cities = {
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
}
for state, abbrev in list(states.items()):
print(f"{state} is abbrviated {abbrev}")
I tried to print the following to understand:
print(states) #output: {'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New York': 'NY', 'Michigan': 'MI'}
print(list(states)) #output: ['Oregon', 'Florida', 'California', 'New York', 'Michigan']
print(states.items()) #output: dict_items([('Oregon', 'OR'), ('Florida', 'FL'), ('California', 'CA'), ('New York', 'NY'), ('Michigan', 'MI')])
print(list(states.items())) #output: [('Oregon', 'OR'), ('Florida', 'FL'), ('California', 'CA'), ('New York', 'NY'), ('Michigan', 'MI')]
to me the list seems to only clear up the sequence.
I tried to Google list(), but still not certain what list exactly does.
When I run the code with out list like the below it also works.
for state, abbrev in states.items():
print(f"{state} is abbrviated {abbrev}")
so why use list in the first place?