How do I delete the Nth list item from a list of lists (column delete)? How do I delete the Nth list item from a list of lists (column delete)? python python

How do I delete the Nth list item from a list of lists (column delete)?


You could loop.

for x in L:    del x[2]

If you're dealing with a lot of data, you can use a library that support sophisticated slicing like that. However, a simple list of lists doesn't slice.


just iterate through that list and delete the index which you want to delete.

for example

for sublist in list:    del sublist[index]


You can do it with a list comprehension:

>>> removed = [ l.pop(2) for l in L ]>>> print L[['a', 'b', 'd'], [1, 2, 4], ['w', 'x', 'z']]>>> print removed['d', 4, 'z']

It loops the list and pops every element in position 2.

You have got list of elements removed and the main list without these elements.