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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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 ]: |