+ 6

How can i literate a nested list ?

Example: li=[1,2[3,4[23,66]],[2,4]] Output 1 2 3 4 23 66 2 4

28th Nov 2018, 1:13 PM
Programmer Raja
Programmer Raja - avatar
7 Answers
+ 15
def fun(l): for x in l: if type(x) is list: fun(x) else: print(x) li = [1,2,[3,4,[23,66]],[2,4]] fun(li)
28th Nov 2018, 1:53 PM
Ipang
+ 16
def chain(lst): for i in lst: if hasattr(i, '__iter__') and type(i) is not str: yield from chain(i) else: yield i print(list(chain( [1, [2, [3, [4, 'abc', [{'key1': 5, 'key2': 6}] ]]]] ))) print(list(chain([1, 2, [3, 4, [23, 66]], [2, 4]])))
28th Nov 2018, 1:48 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 5
Thank you Otterwerks ✌
28th Nov 2018, 7:36 PM
Ipang
+ 4
@ifang you are awesome 👍
29th Nov 2018, 2:52 AM
danvetio
danvetio - avatar
+ 3
lpang 👍 for recursive function
28th Nov 2018, 7:21 PM
Otterwerks
Otterwerks - avatar
+ 3
Thanks to all🙏
29th Nov 2018, 1:06 AM
Programmer Raja
Programmer Raja - avatar
+ 3
And you too PyDan 👍
29th Nov 2018, 5:06 AM
Ipang