How to remove an element from a nested list?

In [5]: m=[[34,345,232],[23,343,342]]

In [7]: [[ subelt for subelt in elt if subelt != 345 ] for elt in m] 
Out[7]: [[34, 232], [23, 343, 342]]

Note that remove(345) only removes the first occurrance of of 345 (if it exists). The above code removes all occurrances of 345.

Leave a Comment