Using pop to remove an element from a 2D array
In the following random array:
a = [[1,2,3,4],
[6,7,8,9]]
Could you please tell me how to remove an item at a specific position. For example, how to remove a[1][3]
?
I understand this list.pop
is only used for list type DS.
+3
Gaara
source
to share
3 answers
Simple, just click on the list item.
>>> a = [[1,2,3,4], [6,7,8,9]]
>>> a[1].pop(3)
>>> a
[[1, 2, 3, 4], [6, 7, 8]]
+5
nathancahill
source
to share
You should use del
to remove an element at a specific index:
>>> a = [[1,2,3,4], [6,7,8,9]]
>>> del a[1][3]
>>> a
[[1, 2, 3, 4], [6, 7, 8]]
>>>
list.pop
should only be used when you need to store the value you just deleted.
+2
iCodez
source
to share
In this case,
a[1].remove(9)
removes [1] [3]
python list document link
-1
ChesterL
source
to share