[image] Leonard Dye: I’m working with lists. I need to sort by one of the values in the sub list. I need the whole sub list to move with the value that is being sorted. /…/ So for being clear I’m sorting 23, 48, 78 There is built-in sorted which accepts key function. Sorted creates new list, … Answer from aivarpaalberg on discuss.python.org
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python sort list of lists
Python Sort List of Lists - Spark By {Examples}
May 31, 2024 - For example, the itemgetter() function along with the sorted() function sorts a list of lists based on the first element of each sub-list. # Import from operator import itemgetter # Using sorted() function # Use itemgetter() function to select ...
🌐
Python documentation
docs.python.org › 3 › howto › sorting.html
Sorting Techniques — Python 3.14.4 documentation
February 23, 2026 - For example, to sort the student data by descending grade and then ascending age, do the age sort first and then sort again using grade: >>> s = sorted(student_objects, key=attrgetter('age')) # sort on secondary key >>> sorted(s, key=attrgetter('grade'), reverse=True) # now sort on primary key, descending [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] This can be abstracted out into a wrapper function that can take a list and tuples of field and order to sort them on multiple passes.
Discussions

Sorting list of lists
I’m working with lists. I need to sort by one of the values in the sub list. I need the whole sub list to move with the value that is being sorted. Example data: files = [asb, 23, 89], [asc, 48, 89], [asa, 78, 89] # Th… More on discuss.python.org
🌐 discuss.python.org
8
0
December 27, 2022
Sorting nested list by 2 attributes / different order?
Provided they are numerical values, remove the reverse=True and negate the value you want to sort descending. For example, sorting by the first element descending, second element ascending: l.sort(key=lambda x: (-x[0], x[1])) More on reddit.com
🌐 r/learnpython
4
1
August 7, 2022
How do I sort a list with sublists with multiple ascending and descending criteria?
I've figured it out. At first I tried -abs(i[0]) but I didn't realize that -|negative number| is still a negative number, so of course the sorting is not going to work. I came up with -i[0] just before I write this post and that's exactly what I need, but I didn't actually test it out myself before posting, so I'm sorry about that. Thanks everyone for helping. Have a great day. More on reddit.com
🌐 r/learnpython
9
2
October 12, 2023
python - Sort list of lists ascending and then descending - Stack Overflow
You cannot do -k[0] as that value is a string. So, reverse=True reverses the sort order and -k[1] cancels out that reversal on the second element. 2011-07-12T15:48:40.413Z+00:00 ... You can do successive rounds of sorting as python's sort is stable. You need to first sort on the secondary key ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-list-according-second-element-sublist
Sort a list according to the second element of sublist - Python - GeeksforGeeks
May 9, 2025 - Explanation: sorted(a, key=lambda x: x[1]) sorts the list a by the second element of each sublist, using a lambda function to specify that the sorting should be done based on the value at index 1 of each sublist.
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch02s05.html
Sorting by One Field, Then by Another - Python Cookbook [Book]
July 19, 2002 - import string star_list = ['Elizabeth Taylor', 'Bette Davis', 'Hugh Grant', 'C. Grant'] star_list.sort(lambda x,y: ( cmp(string.split(x)[-1], string.split(y)[-1]) or # Sort by last name... cmp(x, y))) # ...then by first name print "Sorted list of stars:" for name in star_list: print name
Authors   Alex MartelliDavid Ascher
Published   2002
Pages   608
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-list-of-list-by-specified-index
Python - Sort list of list by specified index - GeeksforGeeks
July 11, 2025 - a = [[1, 'Python', 50], [2, 'is', 30], [3, 'fun!', 40]] # Sorting by 2nd index first, then by 0th index sorted_data = sorted(a, key=lambda x: (x[1], x[0])) print(sorted_data) Output · [[1, 'Python', 50], [3, 'fun!', 40], [2, 'is', 30]] The ...
🌐
Codecademy
codecademy.com › article › how-to-sort-lists-of-lists-in-python
How to Sort Lists of Lists in Python (With Examples) | Codecademy
Python’s built-in sorted() function lets you sort a list of lists by any element you choose. It works by examining a specific position in each sub-list (like the second element for grades) and organizing the data accordingly.
🌐
Reddit
reddit.com › r/learnpython › sorting nested list by 2 attributes / different order?
r/learnpython on Reddit: Sorting nested list by 2 attributes / different order?
August 7, 2022 -

Hello - i would like to sort a nested list for 2 attributes - but the first 1 attribute should be sorted descending and the second ascending -

Its clear for me how to sort both attributes descending with

l.sort(key=lambda x: (x[0], x[1]), reverse=True) 

But how can i sort the first attribute descending and the second atttribut ascending?

Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how do i sort a list with sublists with multiple ascending and descending criteria?
r/learnpython on Reddit: How do I sort a list with sublists with multiple ascending and descending criteria?
October 12, 2023 -

For example, if I have a list :

list = [ [-10, 1, 3], [2, -5, 29], [3, -5, 0], [0, 0, -1] ]

And I want to sort the list with the following criteria in order :

For i in list :

  1. sort i[2] (in ascending order)

  2. sort i[0] (in descending order)

  3. sort i[1] (in ascending order)

I've tried rearranging by using list comprehension and *-1 on the index I want to sort in descending order, then sort the rearranged list ( ex : newlist = [ [i[2], -i[0], i[1]] for i in list ] then newlist.sort() ) However, *-1 for descending order doesn’t work since some numbers are already negative, so that’s like where I’m stuck with. Any idea how to do this?

By the way, I cannot use dict, numpy, tuple, set or import any libraries since this is my college homework and my professor doesn’t allow us to use any of those.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sort-values-first-list-using-second-list
Sort the values of first list using second list in Python - GeeksforGeeks
December 11, 2025 - numpy.argsort() gives the index positions that would sort a list. Using these indices, we can reorder another list accordingly. ... import numpy as np a = ['a', 'b', 'c', 'd'] b = [3, 1, 4, 2] res = [a[i] for i in np.argsort(b)] print(res) ...
🌐
Python Pool
pythonpool.com › home › blog › 6 unique ways in python to sort the list of lists
6 Unique Ways in Python to Sort the List of Lists - Python Pool
June 14, 2021 - Good article! It’s probably worth mentioning that for “Sorting The Data By 1st Column”, if the first elements in each list are identical, then it sorts on the second element on each list, and if the second elements are also identical, it moves on to the third element, etc.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to sort a list of tuples by first and second element in python
5 Best Ways to Sort a List of Tuples by First and Second Element in Python - Be on the Right Side of Change
February 23, 2024 - This code snippet sorts the list of tuples using the default sorting behavior of Python’s sorted() function which naturally sorts based on the first then second element of the tuples.
🌐
GeeksforGeeks
geeksforgeeks.org › python › sorting-list-of-lists-with-first-element-of-each-sub-list-in-python
Sorting List of Lists with First Element of Each Sub-List in Python - GeeksforGeeks
July 23, 2025 - One of the way is to use List comprehension along with the sorted() function to create a new sorted list based on the first element of each sub-list.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to sort list of tuples in python
How to Sort List of Tuples in Python - Spark By {Examples}
May 31, 2024 - To sort the list of tuples by multiple elements in Python, use the lambda expression with the elements you wanted. For instance, the key function lambdax:(x[0],x[1]) returns a tuple of the first and second elements of each tuple, and the sorted() ...