-2
test_list = [[4, 5, 6, 8], 
         [2, 7, 10, 9], 
         [12, 16, 18, 20]] 

If I want to remove column 2, that is [5,7,16] from the list, I know I can use:

[j.pop(1) for j in test_list] 

however, if I want to move 2 columns at the same time, that is [5,7,16] and [8,9,20], how can I change the code, so the result is:

The modified mesh after column deletion : [[4, 6], [2, 10], [12, 18]]
hexdec123
  • 45
  • 7
  • 2
    remove column 1 first, then column 3? – Julien Feb 22 '21 at 04:09
  • 1
    Why not use numpy and [delete columns](https://stackoverflow.com/questions/1642730/how-to-delete-columns-in-numpy-array) with its optimized code? – sedavidw Feb 22 '21 at 04:11
  • 1
    Maybe just pick the columns you want rather than deleting: `list(map(itemgetter(0, 2), test_list))` – Mark Feb 22 '21 at 04:11

5 Answers5

1

Here's one other way:

columns_to_remove = (1,3)
new_object = [[x for i,x in enumerate(l) if i not in columns_to_remove] for l in test_list]

Note that this creates a new object without modifying the original test_list.

Julien
  • 13,986
  • 5
  • 29
  • 53
  • Nope, it doesn't generate the same result. ```[[4, 5, 6, 8], [2, 7, 10, 9], [12, 16, 18, 20]]``` – Nagmat Feb 22 '21 at 04:14
  • Now, it works fine after the code was edited, before it wasn't generating the correct result. – Nagmat Feb 22 '21 at 04:18
0
test_list = [[4, 5, 6, 8], [2, 7, 10, 9],[12, 16, 18, 20]]
removeIndex = [1,3] # The indices you want to remove
for l in test_list:
    for i,v in enumerate(removeIndex):
        l.pop(v-i)
print(test_list)
Wrench
  • 490
  • 4
  • 13
0

You can try list comprehension with required indices NOTE: I am not altering the original list, it is creating brand new list

test_list = [[4, 5, 6, 8], 
         [2, 7, 10, 9], 
         [12, 16, 18, 20]] 

[[i[0], i[2]] for i in test_list]
[[4, 6], [2, 10], [12, 18]]
Epsi95
  • 8,832
  • 1
  • 16
  • 34
0

You can do it easily with numpy:

import numpy as np
test_list = [[4, 5, 6, 8],
             [2, 7, 10, 9],
             [12, 16, 18, 20]]

a = np.array(test_list)
a = np.delete(a, [1,3], axis=1)

print(a)
#output:
[[ 4  6]
 [ 2 10]
 [12 18]]
pakpe
  • 5,391
  • 2
  • 8
  • 23
0

You can delete multiple columns using a numpy array. Please look at numpy.delete() for documentation.

import numpy as np
test_list = [[4, 5, 6, 8], 
         [2, 7, 10, 9], 
         [12, 16, 18, 20]]
a = np.array(test_list)
a = np.delete(a, [1, 3], axis=1)
print (a)

The output will be:

[[ 4  6]
 [ 2 10]
 [12 18]]

You can also use numpy.delete with slice() if you want to remove a column or a set of columns.

If you want to remove 2nd and 3rd column, you can give:

np.delete(a, slice(1, 3), axis=1)

array([[ 4,  8],
       [ 2,  9],
       [12, 20]])

If you want to delete 2 and 4th column, you can use slice(start, stop, skip) option as follows:

np.delete(a, slice(1, None,2), 1)

Output of this will be:

array([[ 4,  6],
       [ 2, 10],
       [12, 18]])

If you want the numpy array to be stored back as a regular list of list, you can always do a.tolist()

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33