Python itertools.chain: loop over several sequences as one
itertools.chain(a, b) returns an iterator that yields the items of a, then the items of b. Only lists, a tuple, a string and one generator were chained. chain.from_iterable(iterable_of_sequences) takes the sequences from one iterable.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-103-A: chain joins a list, a tuple and a string, from_iterable takes its sequences from one list, and the iterator is used up after one pass
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
from itertools import chain
c = chain([1, 2], (3,), 'ab')
print(list(c))
print(list(c))
print(list(chain.from_iterable([[1, 2], [3]])))
EOSCommand:
python3 s.pyRecorded output:
[1, 2, 3, 'a', 'b']
[]
[1, 2, 3]Exit status: 0. The output matches the expected text written before the run.
TE-103-B: chain is lazy: a generator inside it does not start until the first item is requested
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
from itertools import chain
def g():
print('gen start')
yield 1
print('gen end')
yield 2
c = chain(g(), [9])
print('built')
print(next(c))
EOSCommand:
python3 s.pyRecorded output:
built
gen start
1Exit status: 0. The output matches the expected text written before the run.
TE-103-C: expected non-zero exit: chain over an item that is not iterable fails when the items are read, not when chain is called
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
from itertools import chain
c = chain(1, [2])
print('built')
print(list(c))
EOSThe file ../err.txt sits one level above the working folder, inside the same throwaway tree. The output block below shows only the last line of the error; the full traceback went to ../err.txt.
Command:
python3 s.py 2>../err.txt; rc=$?; tail -n 1 ../err.txt; exit $rcRecorded output:
built
TypeError: 'int' object is not iterableExit status: 1. This status and the output match what was written before the run.
Reading the recorded output
The first printed list, [1, 2, 3, 'a', 'b'], shows the items of the list [1, 2], then the tuple (3,), then the text ab, one character at a time, in the order the arguments were given. The second call to list on the same chain object printed [], an empty list, because the first call had already used up the iterator. The third line shows chain.from_iterable taking two lists out of one outer list and giving [1, 2, 3]. A new chain object would be needed to read the items again.
TE-103-B: built was printed first, then gen start, then 1. Calling chain did not start the generator. It started when next(c) asked for the first item, and gen end was not printed because only one item was taken. TE-103-C: built was printed, so chain(1, [2]) was accepted. The error came later, when list(c) read the items: TypeError: 'int' object is not iterable, with exit status 1.
Limits
The result is an iterator, so it is used up after one pass (shown by the second list call). Only lists, a tuple, a string and one generator were chained.
Source
Evidence
TE-103-A: chain joins a list, a tuple and a string, from_iterable takes its sequences from one list, and the iterator is used up after one passTE-103-B: chain is lazy: a generator inside it does not start until the first item is requestedTE-103-C: expected non-zero exit: chain over an item that is not iterable fails when the items are read, not when chain is called
- logs: one recorded log for each row above
- Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-103-A | chain joins a list, a tuple and a string, from_iterable takes its sequences from one list, and the iterator is used up after one pass | 0 | PASS | 290f308621c7 | | TE-103-B | chain is lazy: a generator inside it does not start until the first item is requested | 0 | PASS | 56320234d4ff | | TE-103-C | expected non-zero exit: chain over an item that is not iterable fails when the items are read, not when chain is called | 1 | PASS | 4aa93051e070 |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides