UnicornsOnLSD
UnicornsOnLSD

Reputation: 773

Move item from one 2D list cell to another specific cell in the same list

So, say that I have a 2D list with these items:

foo = [[1,2,3][4,5,6]]

and I want to move foo[1][0] to foo[0][1] to make this output:

foo = [[1,4,2,3][5,6]]

How would I go about doing that?

Upvotes: 0

Views: 307

Answers (2)

thakee nathees
thakee nathees

Reputation: 945

you can simply do this

foo[0].insert(1,foo[1].pop(0))

Upvotes: 1

molamk
molamk

Reputation: 4116

  • Use insert to insert an item into an array into a certain index
  • Use pop to remove and return an item from a list at a certain index

Combine both

foo = [[1,2,3], [4,5,6]]
foo[0].insert(1, foo[1].pop(0))

Upvotes: 1

Related Questions