If you have more than one nested level, this can help.
def nested_remove(L, x): if x in L: L.remove(x) else: for element in L: if type(element) is list: nested_remove(element, x) >>> m=[[34,345,232],[23,343,342]] >>> nested_remove(m, 345) >>> m [[34, 232], [23, 343, 342]] >>> m=[[34,[345,56,78],232],[23,343,342]] >>> nested_remove(m, 345) >>> m [[34, [56, 78], 232], [23, 343, 342]]
source share