python-cookbook
python-cookbook copied to clipboard
Missing ignore_types arg when calling the method of flatten recursively
The code has missed ignore_types arg when calling the method of flatten
recursively in the example about how to flatten a nested sequence.
src/4/how_to_flatten_a_nested_sequence/example.py
from collections import Iterable
def flatten(items, ignore_types=(str, bytes)):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, ignore_types):
yield from flatten(x)
else:
yield x
The corrected code is yield from flatten(x, ignore_types)
.