Iterating Over Dictionaries
print(state) will print
"New York", and
"Texas". What if you also want to print the city? Well, you already know how to access a value if you know its key, so you can adjust the
print() call and write
Instead of having
for state in capitals in line 7, you can now write
for state, city in capitals.items():
print(state and now instead of
[state], you can print the
city. When you save and run it, then the output is exactly the same like before, but this code is considered more Pythonic.
Become a Member to join the conversation.