Skip to content
Advertisement

Iterating a dictionary with list values (why does it iterate over the list)

I’m pretty new to programming and I’m starting with python

Why is it that the ‘for’ loop is iterating over the list and not over the key of the dictionary?

The intention is that the loop iterates over the list but I don’t understand why it’s happening if ‘for’ loops are supposed to iterate over the keys and not over the values

JavaScript

Advertisement

Answer

When you iterate over a dict like so:

JavaScript

You actually do this:

JavaScript

To get the key and the value, simply do this:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement