I can't imagine how is possible to get this error when I use the function on such a small data
How is that even possible?

How is that even possible?
In [1]: l = [list(word) for word in 'Something goes wrong'.split()] In [2]: def flatten(iterable): ...: iterator = iter(iterable) ...: for item in iterator: ...: if '__iter__' in dir(item): ...: yield from flatten(item) ...: else: ...: yield item ...: In [3]: flat = [item for item in flatten(l)] --------------------------------------------------------------------------- RecursionError Traceback (most recent call last) <ipython-input-3-930115f14cc4> in <module>() ----> 1 flat = [item for item in flatten(l)] <ipython-input-3-930115f14cc4> in <listcomp>(.0) ----> 1 flat = [item for item in flatten(l)] <ipython-input-2-7311442c70ce> in flatten(iterable) 3 for item in iterator: 4 if '__iter__' in dir(item): ----> 5 yield from flatten(item) 6 else: 7 yield item ... last 1 frames repeated, from the frame below ... <ipython-input-2-7311442c70ce> in flatten(iterable) 3 for item in iterator: 4 if '__iter__' in dir(item): ----> 5 yield from flatten(item) 6 else: 7 yield item RecursionError: maximum recursion depth exceeded while calling a Python object In [4]: In [4]: flat = [item for item in flatten(l)] --------------------------------------------------------------------------- RecursionError Traceback (most recent call last) <ipython-input-4-930115f14cc4> in <module>() ----> 1 flat = [item for item in flatten(l)] <ipython-input-4-930115f14cc4> in <listcomp>(.0) ----> 1 flat = [item for item in flatten(l)] <ipython-input-2-7311442c70ce> in flatten(iterable) 3 for item in iterator: 4 if '__iter__' in dir(item): ----> 5 yield from flatten(item) 6 else: 7 yield item ... last 1 frames repeated, from the frame below ... <ipython-input-2-7311442c70ce> in flatten(iterable) 3 for item in iterator: 4 if '__iter__' in dir(item): ----> 5 yield from flatten(item) 6 else: 7 yield item RecursionError: maximum recursion depth exceeded while calling a Python object In [5]: