Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swap first 2 rows of a 2d Python list without Numpy?

How can I swap the first 2 rows of a 2D Python list without using Numpy?
For example, if my list is:

lst = [[0,3,2],
       [4,3,2],
       [3,5,2]]

I want to swap [0,3,2] with [4,3,2].

Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Something like this? Or do you need it to be a function to swap when needed?

lst = [[0,3,2],
       [4,3,2],
       [3,5,2]]

lst[0], lst[1] = lst[1], lst[0]
print(lst)
# [[4, 3, 2], 
# [0, 3, 2], 
# [3, 5, 2]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading