How do I delete a "column" from a list of lists?
Given:
L = [
["a","b","C","d"],
[ 1, 2, 3, 4 ],
["w","x","y","z"]
]
I would like to delete "column" 2 to get:
L = [
["a","b","d"],
[ 1, 2, 4 ],
["w","x","z"]
]
Is there a slice or del method that will do that? Something like:
del L[:][2]
You could loop.
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.
If you don't mind on creating new list then you can try the following:
It works fine.
just iterate through that list and delete the index which you want to delete.
for example
You can do it with a list comprehension:
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.
This is a very easy way to remove whatever column you want.