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.