6

In the below random array:

a = [[1,2,3,4],
     [6,7,8,9]] 

Could you please tell me how to remove element at a specific position. For example, how would I remove a[1][3]?

I understand list.pop is used for only list type DS here.

Gaara
  • 695
  • 3
  • 8
  • 23

4 Answers4

7

Simple, just pop 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]]
nathancahill
  • 10,452
  • 9
  • 51
  • 91
2

You should use del to remove an item 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 save the value you just removed.

  • If we remove one element from a column , for example , from bottom of a column, then how to adjust the element in that column ? – Bravo Oct 27 '17 at 11:24
0

You can use any of the three method:

  1. Remove
  2. Pop
  3. del

a = [[1,2,3,4], [6,7,8,9]]

1- Remove a[1].remove(a[1][3])

2- Pop a[1].pop(3)

3-Del del a[1][3]

  • To make your answer clearer, have a look at using code blocks - see the 'help' option while writing/editing your answer. – dspencer Mar 15 '20 at 15:00
-1

In this case, a[1].remove(9) removes a[1][3]

link to python list document

ChesterL
  • 347
  • 1
  • 3
  • 11