1

I have a list of lists. I want to pop() an element say from the second list in the list of lists. Here is an example:

>>> list1=[1,2]
>>> list2=[3,4]
>>> listoflists=[list1, list2]

so, printing listoflists gives me:

>>>listoflists
[[1, 2], [3, 4]]

I want to pop, say, the first element of the second list in listoflists, i.e., 3.

>>>listoflists.pop([1][0])

gives me the following error;

Traceback (most recent call last):

  File "<ipython-input-14-db5dc303028d>", line 1, in <module>
    listoflists.pop([0][1])

IndexError: list index out of range
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
vahid
  • 315
  • 5
  • 13

2 Answers2

2
listoflists[1].pop(0)

listoflists[1] equals list2

so

listoflists[1].pop(0) equals list2.pop(0)

Ksir
  • 396
  • 2
  • 5
1

the correct way to pop 2d arrays is like this

list1=[1,2]
list2=[3,4]
listoflists=[list1, list2]

print listoflists

listoflists[0].pop(0)//correct way to pop

print listoflists

here is another post similar to yours on poping 2d lists that also might be of use.

Community
  • 1
  • 1
Ryan
  • 1,972
  • 2
  • 23
  • 36