In the expression:
for foo in itertools.combinations(range(1, 8), 4)
itertools.combinations(range(1, 8), 4)
is yielding sequences of 4 items, which are being assigned to the variable foo
. You can see this if you just print foo
in a loop:
>>> for foo in itertools.combinations(range(1, 8), 4):
... print(foo)
...
(1, 2, 3, 4)
(1, 2, 3, 5)
(1, 2, 3, 6)
(1, 2, 3, 7)
(1, 2, 4, 5)
(1, 2, 4, 6)
(1, 2, 4, 7)
(1, 2, 5, 6)
(1, 2, 5, 7)
(1, 2, 6, 7)
(1, 3, 4, 5)
(1, 3, 4, 6)
(1, 3, 4, 7)
(1, 3, 5, 6)
(1, 3, 5, 7)
(1, 3, 6, 7)
(1, 4, 5, 6)
(1, 4, 5, 7)
(1, 4, 6, 7)
(1, 5, 6, 7)
(2, 3, 4, 5)
(2, 3, 4, 6)
(2, 3, 4, 7)
(2, 3, 5, 6)
(2, 3, 5, 7)
(2, 3, 6, 7)
(2, 4, 5, 6)
(2, 4, 5, 7)
(2, 4, 6, 7)
(2, 5, 6, 7)
(3, 4, 5, 6)
(3, 4, 5, 7)
(3, 4, 6, 7)
(3, 5, 6, 7)
(4, 5, 6, 7)
If you replace foo
with a list slice that is 4 elements long, then those 4 elements are instead assigned to that slice:
>>> bars = [0, 0, 0, 0, 0, 8]
>>> for bars[1:-1] in itertools.combinations(range(1, 8), 4):
... print(bars)
...
[0, 1, 2, 3, 4, 8]
[0, 1, 2, 3, 5, 8]
[0, 1, 2, 3, 6, 8]
[0, 1, 2, 3, 7, 8]
[0, 1, 2, 4, 5, 8]
[0, 1, 2, 4, 6, 8]
[0, 1, 2, 4, 7, 8]
[0, 1, 2, 5, 6, 8]
[0, 1, 2, 5, 7, 8]
[0, 1, 2, 6, 7, 8]
[0, 1, 3, 4, 5, 8]
[0, 1, 3, 4, 6, 8]
[0, 1, 3, 4, 7, 8]
[0, 1, 3, 5, 6, 8]
[0, 1, 3, 5, 7, 8]
[0, 1, 3, 6, 7, 8]
[0, 1, 4, 5, 6, 8]
[0, 1, 4, 5, 7, 8]
[0, 1, 4, 6, 7, 8]
[0, 1, 5, 6, 7, 8]
[0, 2, 3, 4, 5, 8]
[0, 2, 3, 4, 6, 8]
[0, 2, 3, 4, 7, 8]
[0, 2, 3, 5, 6, 8]
[0, 2, 3, 5, 7, 8]
[0, 2, 3, 6, 7, 8]
[0, 2, 4, 5, 6, 8]
[0, 2, 4, 5, 7, 8]
[0, 2, 4, 6, 7, 8]
[0, 2, 5, 6, 7, 8]
[0, 3, 4, 5, 6, 8]
[0, 3, 4, 5, 7, 8]
[0, 3, 4, 6, 7, 8]
[0, 3, 5, 6, 7, 8]
[0, 4, 5, 6, 7, 8]
Same 4 elements, but now they're assigned to the middle of bars
, as referenced by the slice expression bars[1:-1]
.
In the context of your comprehension, bars
will have those values as it goes through the outer loop. The inner comprehension generates another list that is based on the value of bars
in that outer loop.