Ex 39 - What does list() exactly do?

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?

Yes, that was an accident and you can just ditch it. I think I had done list at one point for some other reason then forgot to remove it.

1 Like

Great then it makes sense to me;)