Home > Enterprise >  Remove 0 values in a list of list python
Remove 0 values in a list of list python

Time:06-19

I need to remove the 0 values, but I don't know how, someone can help me please?

coord = [[(1.0, 1.0), (4.0, 2.0), 0, 0, 0, 0, (1.0, 5.0), 0, 0, (3.0, 3.0)], [(1.0, 1.0), (4.0, 2.0), 0, 0, 0, 0, (1.0, 5.0), 0, 0, (3.0, 3.0)]]

I need that coord be:

coord = [[(1.0, 1.0), (4.0, 2.0), (1.0, 5.0), (3.0, 3.0)], [(1.0, 1.0), (4.0, 2.0), (1.0, 5.0), (3.0, 3.0)]]

CodePudding user response:

You can do it with a list comprehension:

[[i for i in j if i] for j in coord]

Output:

[[(1.0, 1.0), (4.0, 2.0), (1.0, 5.0), (3.0, 3.0)],
 [(1.0, 1.0), (4.0, 2.0), (1.0, 5.0), (3.0, 3.0)]]

CodePudding user response:

Use a list comprehension:

[[item for item in sublist if item != 0] for sublist in coord]

This outputs:

[
 [(1.0, 1.0), (4.0, 2.0), (1.0, 5.0), (3.0, 3.0)],
 [(1.0, 1.0), (4.0, 2.0), (1.0, 5.0), (3.0, 3.0)]
]
  • Related