0

I'm looking to take a list of lists and sort them by an element contained in the contained lists. For clarification, I want to have a list like this code

myList = []
myList.append([1, 2, 3])
myList.append([1, 1, 1])
myList.append([1, 3, 2])

and then sort the elements of list by the last element of each contained list and it would end up looking like this

           |
myList = ( v
    [1, 2, 3],
    [1, 3, 2],
    [1, 1, 1])

I also have the numpy library imported in this project. I was looking at argsort for numpy but I don't think you can sort it along an axis of an element

JohnMadden
  • 41
  • 5

1 Answers1

2

First of all don't use list as variable name as it is one of python built-in function, and second you can use sorted function to sort by position

l = []
l.append([1, 2, 3])
l.append([1, 1, 1])
l.append([1, 3, 2])
sorted(l,key = lambda x : x[2])
#[[1, 1, 1], [1, 3, 2], [1, 2, 3]]
ExplodingGayFish
  • 2,807
  • 1
  • 5
  • 14