0

If i have the matrix:

[[1,1,1],[2,3,4],[4,5,6],[7,1,2]]

I want to swap rows 1 and 2 so that it returns:

[[1,1,1],[4,5,6],[2,3,4],[7,1,2]]
john
  • 81
  • 1
  • 4
  • 8

1 Answers1

2

You can do this the same way you do any other swap in python:

i.e. a, b = b, a -- or in your case:

>>> matrix = [[1,1,1],[2,3,4],[4,5,6],[7,1,2]]
>>> matrix[1], matrix[2] = matrix[2], matrix[1]
>>> matrix
[[1, 1, 1], [4, 5, 6], [2, 3, 4], [7, 1, 2]]
mgilson
  • 300,191
  • 65
  • 633
  • 696