0

lets say i have a list which contains lists and integers. I want to write a comprehension where i get a single list of all elements as individual integers. Eg lst = [3,4,5,6,4,[2,3,4,9],[2,3,4]] which i want to convert into a single list [3,4,5,6,4,2,3,4,9,2,3,4]. Best I could manage so far is:

lst = [3,4,5,6,4,[2,3,4,9],[2,3,4]]
a = [i for i in lst if type(i)==int]
b = [j for i in lst if type(i)==list for j in i ]
x = a + b

is there a way to do this in a single list comprehension? Furthermore is there a convenient way to get a single list regardless of the input list structure? eg. inputList = [1,[2,3],[2,[3,4]]] outputList = [1,2,3,2,3,4] Thanks in advance for response.

0 Answers0