0

I currently have an list (list1) that looks like this:

[[0],
 [2],
 [4],
 [6],
 [7],
 [9],
 [20],
 [24],
 [],
 [26],
 [],
 [27],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 []] 

and I would like to transform it to look like this, but I am having trouble:

[0, 2, 4, 6, 7, 9, 20, 24, 26, 27]

Any help is appreciated- thank you!

youtube
  • 105
  • 5

2 Answers2

1

Just use a list comprehension:

out = [l[0] for l in list1 if len(l) > 0]
Eelco van Vliet
  • 878
  • 8
  • 15
0

Assuming list1 the input list, use itertools.chain:

from itertools import chain

out = list(chain.from_iterable(list1))

output: [0, 2, 4, 6, 7, 9, 20, 24, 26, 27]

mozway
  • 81,317
  • 8
  • 19
  • 49