0

Since my text book tutorial on lists and for loops, I have come to see for loops written with more complexity than the type that were explained:

for i in list:

Often times I will see something like

for j, k in l, ll:

and that's just one example of varied syntax for loop I have seen.

Let's use this example to demonstrate how I've tried to explain to myself in the shell how to read these multiple variable/multiple list for loops.

>>> l
['one', 'two']
>>> ll
['b', 'a']

With these values, I can do:

>>> for j, k in l, ll:
...     print(j, k)
...     print("increment")
... 
one two
increment
b a
increment

Okay, so I tried l.append("three") and running the same lines back in the shell. This results in this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

I want to understand in English how to read the syntax when multiple values are given for vars/lists in a for loop, and I just don't get it.


I have gathered from reading other questions that the iterable j, k is a tuple, which makes sense now that I think about it. But I still don't understand what is going on when there are multiple lists, probably to be understood as a tuple of two lists as well.

I believe that what I am struggling to understand about for-loops is described in the documentation as "list comprehension," which is amusing since I am not comprehending it.

If anyone can make this more clear at the highest level, that would be great.

  • 1
    If there are three items in `l`, `j, k = l` doesn't make sense: `j, k = ['one', 'two', 'three]` - where does the third item go? Hence: `ValueError: too many values to unpack `. `for ... in l, ll:` just means you're iterating over a tuple (...of lists of strings). And this has nothing to do with list comprehensions; there isn't one in the code you've posted. – jonrsharpe Jun 11 '19 at 15:46

2 Answers2

0

The way to read this statement:

for j, k in l, ll:
    print(j, k)

is that l,ll is treated as a tuple containing two lists with each list expected to have exactly two elements (which will be unpacked to loop variables j and k).

It would be the same as writing:

for j, k in (l, ll):
    print(j, k)

The j, k part of the for loop "unpacks" the content of each list so these list MUST have exactly two elements.

The number of iterations corresponds to the number of lists provided. You could add a third list but it will need to also have two elements. You cannot add elements to these lists because they would not fit into the j,k assignment of the for loop

lll = ['x','y']
for j,k in l, ll, lll:
    print(j,k)

# one two
# b a
# x y
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

When you write for i in a, b:, it is the same as for i in (a, b):

Let's take a closer look. Imagine you have this code:

array = [(1, 2), (3, 4), (5, 6)]
for i in array:
    print(i)

It's easy to understand that output of this code is this:

(1, 2)
(3, 4)
(5, 6)

You iterate throw a list of tuples. Now, in Python, there is a syntax to get the values of a tuple in different variables like this:

i, j = (1, 2)

which is the same as

i, j = 1, 2

So you could also have written your loop like this:

for i, j in a, b:
    print(i, j)

And get the same output:

1 2
3 4
5 6

But now imagine that you change your array like this

array = [(1, 2), (3, 4), (5, 6, 7)]

Python won't be able to figure out what you mean when you ask him to get i and j from (5, 6, 7) and that's the cause of the ValueError.

Rostan
  • 809
  • 9
  • 25