0

I currently have a list that looks like this:

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

How can I transform it to look like [0, 2, 4, 6, 7, 9, 20, 24, 26, 27]?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
youtube
  • 265
  • 1
  • 7

2 Answers2

1

Just use a list comprehension:

out = [l[0] for l in list1 if len(l) > 0]
Eelco van Vliet
  • 1,198
  • 12
  • 18
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
  • 194,879
  • 13
  • 39
  • 75