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

9

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]
Brindled answered 6/11, 2012 at 4:36 Comment(0)
H
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.

Hermanhermann answered 6/11, 2012 at 4:38 Comment(2)
how do I save the resulting newlist into a new variable?Watertight
@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.Hermanhermann
H
7

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

for example

for sublist in list:
    del sublist[index]
Hesperian answered 6/11, 2012 at 4:40 Comment(2)
this solution is slowPrudery
Could you please then let me know what is the best ever solution for this Prob :)Hesperian
L
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.

Laruelarum answered 10/2, 2016 at 11:25 Comment(0)
I
4

A slightly twisted version:

index = 2  # Delete column 2 
[(x[0:index] + x[index+1:]) for x in L]
Italia answered 6/11, 2012 at 4:53 Comment(0)
N
2
[(x[0], x[1], x[3]) for x in L]

It works fine.

Nosewheel answered 6/11, 2012 at 9:34 Comment(0)
I
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']]
Incoercible answered 6/11, 2012 at 4:47 Comment(0)
C
1
L = [['a', 'b', 'C', 'd'], [1, 2, 3, 4], ['w', 'x', 'y', 'z']]
_ = [i.remove(i[2]) for i in L]
Cutie answered 6/11, 2012 at 16:29 Comment(1)
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].Fustigate
F
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)
Fillender answered 6/11, 2012 at 4:46 Comment(0)
F
0

An alternative to pop():

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

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

Foulup answered 16/6, 2019 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.