20

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]
Georgy
  • 12,464
  • 7
  • 65
  • 73
P Moran
  • 1,624
  • 3
  • 18
  • 32

9 Answers9

14

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.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • how do I save the resulting newlist into a new variable? – DJ_Stuffy_K Mar 22 '18 at 19:14
  • 2
    @DJ_Stuffy_K: This technique doesn't create new lists, it modifies the existing lists. However, you can copy the lists beforehand. `new_L = [list(row) for row in L]`... this is just one way to do things, it will create a shallow copy of each row, so when you modify `new_L` the original `L` will be unmodified. – Dietrich Epp Mar 22 '18 at 19:39
7

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

for example

for sublist in list:
    del sublist[index]
Myjab
  • 924
  • 1
  • 7
  • 22
5

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.

jotacor
  • 2,878
  • 1
  • 18
  • 19
4

A slightly twisted version:

index = 2  # Delete column 2 
[(x[0:index] + x[index+1:]) for x in L]
Georgy
  • 12,464
  • 7
  • 65
  • 73
Swapnil
  • 407
  • 4
  • 7
2
[(x[0], x[1], x[3]) for x in L]

It works fine.

ρss
  • 5,115
  • 8
  • 43
  • 73
ParaMeterz
  • 9,507
  • 1
  • 19
  • 21
1

This is a very easy way to remove whatever column you want.

L = [
["a","b","C","d"],
[ 1,  2,  3,  4 ],
["w","x","y","z"]
]
temp = [[x[0],x[1],x[3]] for x in L] #x[column that you do not want to remove]
print temp
O/P->[['a', 'b', 'd'], [1, 2, 4], ['w', 'x', 'z']]
Heroic
  • 980
  • 4
  • 12
1
L = [['a', 'b', 'C', 'd'], [1, 2, 3, 4], ['w', 'x', 'y', 'z']]
_ = [i.remove(i[2]) for i in L]
Georgy
  • 12,464
  • 7
  • 65
  • 73
raton
  • 418
  • 5
  • 14
  • Aside from the fact that using a listcomp for its side effects is generally disfavoured, this won't work unless the element you're removing happens to be first in the list. For example, if one of the sublists is [1,2,1,3], then the `remove` will return `[2,1,3]`, not `[1,2,3]`. – DSM Nov 06 '12 at 16:32
0

If you don't mind on creating new list then you can try the following:

filter_col = lambda lVals, iCol: [[x for i,x in enumerate(row) if i!=iCol] for row in lVals]

filter_out(L, 2)
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
0

An alternative to pop():

[x.__delitem__(n) for x in L]

Here n is the index of the elements to be deleted.

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45