cavezonk
cavezonk

Reputation: 49

Remove a sublist if all the elements are 0

I have this list

lst = [ [2,0,1], [0,0,0], [3,2,4], [0,0,0,0] ]

I'm looking for a way to remove those lists that only have numbers 0

So the expected output is:

lst = [ [2,0,1], [3,2,4] ]

Upvotes: 0

Views: 135

Answers (2)

ksbg
ksbg

Reputation: 3284

Using list comprehension and any:

[l for l in lst if any(l)]

Upvotes: 3

OneCricketeer
OneCricketeer

Reputation: 191864

Try this list comprehension

[l for l in lst if not all(x == 0 for x in l)] 

Upvotes: 0

Related Questions