How to sort a list of lists by a specific index of the inner list?

PythonSorting

Python Problem Overview


I have a list of lists. For example,

[[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]

If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?

Python Solutions


Solution 1 - Python

This is a job for itemgetter

>>> from operator import itemgetter
>>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> sorted(L, key=itemgetter(2))
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]

It is also possible to use a lambda function here, however the lambda function is slower in this simple case

Solution 2 - Python

in place

>>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> l.sort(key=lambda x: x[2])

not in place using sorted:

>>> sorted(l, key=lambda x: x[2])

Solution 3 - Python

Itemgetter lets you to sort by multiple criteria / columns:

sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))

Solution 4 - Python

multiple criteria can also be implemented through lambda function

sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))

Solution 5 - Python

array.sort(key = lambda x:x[1])

You can easily sort using this snippet, where 1 is the index of the element.

Solution 6 - Python

Like this:

import operator
l = [...]
sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))

Solution 7 - Python

I think lambda function can solve your problem.

old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]

#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])

#Resulst of new_list will be:

[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]

Solution 8 - Python

More easy to understand (What is Lambda actually doing):

ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
def thirdItem(ls):
    #return the third item of the list
    return ls[2]
#Sort according to what the thirdItem function return 
ls2.sort(key=thirdItem)

Solution 9 - Python

**old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
    #let's assume we want to sort lists by last value ( old_list[2] )
    new_list = sorted(old_list, key=lambda x: x[2])**

correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?

Solution 10 - Python

Sorting a Multidimensional Array [execute here][1]

points=[[2,1],[1,2],[3,5],[4,5],[3,1],[5,2],[3,8],[1,9],[1,3]]

def getKey(x):
   return [x[0],-x[1]]

points.sort(key=getKey)

print(points)

Solution 11 - Python

Make sure that you do not have any null or NaN values in the list you want to sort. If there are NaN values, then your sort will be off, impacting the sorting of the non-null values.

Check out https://stackoverflow.com/questions/4240050/python-sort-function-breaks-in-the-presence-of-nan

Solution 12 - Python

Using a custom key function you can easily sort any list of lists as you want:

L = [[0,1,'f'], [4,2,'t'], [9,4,'afsd']]

def sorter(lst):
    return lst[2].casefold()

L.sort(key=sorter)

# result: [[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionoldspiceView Question on Stackoverflow
Solution 1 - PythonJohn La RooyView Answer on Stackoverflow
Solution 2 - PythonmouadView Answer on Stackoverflow
Solution 3 - PythonfiderView Answer on Stackoverflow
Solution 4 - PythonRahul KumarView Answer on Stackoverflow
Solution 5 - PythonAbhiView Answer on Stackoverflow
Solution 6 - PythonJim BrissomView Answer on Stackoverflow
Solution 7 - PythonTushar NirasView Answer on Stackoverflow
Solution 8 - PythonAghiad AlzeinView Answer on Stackoverflow
Solution 9 - PythonEgmontDeVosView Answer on Stackoverflow
Solution 10 - PythonNishanView Answer on Stackoverflow
Solution 11 - PythonNicoNuView Answer on Stackoverflow
Solution 12 - PythonThomas Juul DyhrView Answer on Stackoverflow