Search code examples
pythonlistflatten

How to extract non-empty elements from python for number list


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]?


Solution

  • Just use a list comprehension:

    out = [l[0] for l in list1 if len(l) > 0]