-1

At the moment I'm learning Python from Python Crash Course. I understand that for a for loop has a general format like so:

for [iterating variable] in [sequence]:
        [do something]

But as I'm reading the textbook I see this new format in the front of the for loop and I was looking up solutions but I didn't know what this might be called.

for key, value in user_0.items():

I assumed the for loop is designating itself specifically to the dictionary key and that is why there was the word "key" in front of the for loop? Here's a screenshot for a frame of reference if needed.

Picture reference

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Gaimo
  • 11
  • 1

1 Answers1

1

If user_0 is a dict then it has a method items() that returns an iterable view that iterates over the (key, value) tuple.

e.g.

In [1]: a = {1: 2, 3: 4, 5: 6}

In [2]: list(a.items())
Out[2]: [(1, 2), (3, 4), (5, 6)]

It is a handy way of iterating thru both the key and the value of a dict.

Kevin He
  • 1,210
  • 8
  • 19