A key can be a function that returns a tuple:

s = sorted(s, key = lambda x: (x[1], x[2]))

Or you can achieve the same using itemgetter (which is faster and avoids a Python function call):

import operator
s = sorted(s, key = operator.itemgetter(1, 2))

And notice that here you can use sort instead of using sorted and then reassigning:

s.sort(key = operator.itemgetter(1, 2))
Answer from Mark Byers on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-a-list-of-objects-by-multiple-attributes-in-python
Sort a list of objects by multiple attributes in Python - GeeksforGeeks
July 23, 2025 - The lambda function gives the user the freedom to sort by multiple attributes. sorted(defined_list, key = lambda x: (x[column_number_1], x[column_number_2]))
Discussions

How to sort a dictionary where keys have multiple values?

Use a lambda as your sort function.

# lambda r: r[1][2]
# r == ('John Adams', ('111223333', 'A', 91.0))
# r[1] == ('111223333', 'A', 91.0)
# r[1][2] == 91.0
#
d = {
    'John Adams': ('111223333', 'A', 91.0),
    'Willy Smith Jr.': ('222114444', 'C', 77.55),
    'Phil Jordan': ('777886666', 'F', 59.5)
}

for key, value in sorted(d.items(), key=lambda r: r[1][2]):
    print(key, value)

Produces:

('Phil Jordan', ('777886666', 'F', 59.5))
('Willy Smith Jr.', ('222114444', 'C', 77.55))
('John Adams', ('111223333', 'A', 91.0))

By the way, if you end up with multiple entries sharing the same sort value, you can build a compound sort (secondary, tertiary, etc). Your lambda should just return a tuple: lambda r: (r[1][2], r[0]), which would sort by that last value, and then by name if multiple values equal each other.

Here's without lambda:

def key_func(key_value_tuple):
    name = key_value_tuple[0]
    long_str_num, a_to_f, number = key_value_tuple[1]
    return (number, name)

d = {
    'John Adams': ('111223333', 'A', 91.0),
    'Willy Smith Jr.': ('222114444', 'C', 77.55),
    'Phil Jordan': ('777886666', 'F', 59.5)
}

for key, value in sorted(d.items(), key=key_func):
    print(key, value)

Edit: Added non-lambda option.

More on reddit.com
🌐 r/learnpython
9
10
December 12, 2018
sorting with key=lambda
pair is a tuple consisting of two items. Index 0 is the first item, index 1 is the second item. There is no other item in the tuple for any other index value to be valid. More on reddit.com
🌐 r/learnpython
11
1
March 14, 2023
🌐
Python documentation
docs.python.org › 3 › howto › sorting.html
Sorting Techniques — Python 3.14.4 documentation
February 23, 2026 - >>> student_tuples = [ ... ('john', 'A', 15), ... ('jane', 'B', 12), ... ('dave', 'B', 10), ... ] >>> sorted(student_tuples, key=lambda student: student[2]) # sort by age [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
🌐
GeeksforGeeks
geeksforgeeks.org › python › sort-list-of-dictionaries-python-by-multiple-keys
Sort List of Dictionaries by Multiple Keys - Python - GeeksforGeeks
July 23, 2025 - It can be used with a custom sorting key to sort dictionaries based on multiple keys. By passing a lambda function we can sort the list of dictionaries based on multiple keys such as age and score.
🌐
Reddit
reddit.com › r/learnpython › how to sort a dictionary where keys have multiple values?
r/learnpython on Reddit: How to sort a dictionary where keys have multiple values?
December 12, 2018 -

So say this is my dictionary: {'John Adams': ('111223333', 'A', 91.0), 'Willy Smith Jr.': ('222114444', 'C', 77.55), 'Phil Jordan': ('777886666', 'F', 59.5)} and i want to sort it by the third value of each key (eg the 91.0 for John Adams). How would I go about doing that?

Top answer
1 of 4
8

Use a lambda as your sort function.

# lambda r: r[1][2]
# r == ('John Adams', ('111223333', 'A', 91.0))
# r[1] == ('111223333', 'A', 91.0)
# r[1][2] == 91.0
#
d = {
    'John Adams': ('111223333', 'A', 91.0),
    'Willy Smith Jr.': ('222114444', 'C', 77.55),
    'Phil Jordan': ('777886666', 'F', 59.5)
}

for key, value in sorted(d.items(), key=lambda r: r[1][2]):
    print(key, value)

Produces:

('Phil Jordan', ('777886666', 'F', 59.5))
('Willy Smith Jr.', ('222114444', 'C', 77.55))
('John Adams', ('111223333', 'A', 91.0))

By the way, if you end up with multiple entries sharing the same sort value, you can build a compound sort (secondary, tertiary, etc). Your lambda should just return a tuple: lambda r: (r[1][2], r[0]), which would sort by that last value, and then by name if multiple values equal each other.

Here's without lambda:

def key_func(key_value_tuple):
    name = key_value_tuple[0]
    long_str_num, a_to_f, number = key_value_tuple[1]
    return (number, name)

d = {
    'John Adams': ('111223333', 'A', 91.0),
    'Willy Smith Jr.': ('222114444', 'C', 77.55),
    'Phil Jordan': ('777886666', 'F', 59.5)
}

for key, value in sorted(d.items(), key=key_func):
    print(key, value)

Edit: Added non-lambda option.

2 of 4
5

Here's an extension of u/totallygeek's non-lambda solution, using a more advanced Python feature:

d = {'John Adams': ('111223333', 'A', 91.0), 'Willy Smith Jr.': ('222114444', 'C', 77.55),
     'Phil Jordan': ('777886666', 'F', 59.5)}

def sortindex(index):
    def key_func(item):
        key, value = item
        return (value[index], key)
    return key_func

for key, value in sorted(d.items(), key=sortindex(2)):
    print(key, value)

Here, key_func is wrapped inside another function, sortindex, which returns key_func. In other words, the code above does the same thing as this

def key_func(item):
    key, value = item
    return (value[2], key)

for key, value in sorted(d.items(), key=key_func):
    print(key, value)

But notice that the sortindex function allows us to keep the tuple index as a free variable, so that we can also sort on sortindex(0) or sortindex(1).

This technique of wrapping one function inside another is called a closure.

🌐
freeCodeCamp
freecodecamp.org › news › lambda-sort-list-in-python
Lambda Sorted in Python – How to Lambda Sort a List
March 16, 2023 - In this article, you’ll learn how to sort a list with the lambda function. ... You can sort a list with the sort() method and sorted() function. The sort() method takes two parameters – key and reverse.
Find elsewhere
🌐
Real Python
realpython.com › sort-python-dictionary
Sorting a Python Dictionary: Values, Keys, and More – Real Python
December 14, 2024 - To sort a Python dictionary by its keys, you use the sorted() function combined with .items(). This approach returns a list of tuples sorted by keys, which you can convert back to a dictionary using the dict() constructor.
🌐
Bacancy Technology
bacancytechnology.com › qanda › python › use-lambda-for-sorting-in-python
How to Use Lambda for Sorting in Python: A Quick Guide
January 20, 2025 - Fix: To resolve the issue, you should pass the lambda function correctly as the key argument to sorted(). Here’s the corrected code: a = sorted(a, key=lambda x: x.modified, reverse=True) Explanation: key=lambda x: x.modified tells Python to use the modified attribute of each element in the list a as the sorting criterion.
🌐
Python
docs.python.org › 3.10 › howto › sorting.html
Sorting HOW TO — Python 3.10.20 documentation
>>> 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.
🌐
Blogboard
blogboard.io › blog › knowledge › python-sorted-lambda
Python sort lambda - How to sort with lambda key function in Python
February 5, 2023 - Sorting in Python is simple. You'll pass a list, dict or set to the built-in function sorted. Using the key parameter allows us to modify the default sorting behavior.
🌐
LabEx
labex.io › tutorials › python-how-to-use-a-lambda-function-for-custom-sorting-in-python-415786
How to use a lambda function for custom sorting in Python | LabEx
Notice how we used tuples within the lambda function to define multiple sorting criteria. The first element in the tuple is the primary sorting key, and subsequent elements are used for tie-breaking.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use the key Argument in Python (sorted, max, etc.) | note.nkmk.me
August 13, 2023 - Note that this will not work if run as a normal Python script. ... %%timeit sorted(l, key=lambda x: x['k1']) # 1.09 ms ± 35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) %%timeit sorted(l, key=operator.itemgetter('k1')) # 716 µs ± 28.2 µs per loop (mean ± std.
🌐
YouTube
youtube.com › pymoondra
Python tutorials: Sorting (reverse, key, multiple attributes / value) - YouTube
#LetslearnPython #sort python list #Python advanced sorting #Python sort key In this video we will discuss some of the more advanced features of sorting in P...
Published   March 21, 2019
Views   6K
🌐
Finxter
blog.finxter.com › 5-best-ways-to-sort-a-list-of-tuples-by-multiple-keys-in-python
5 Best Ways to Sort a List of Tuples by Multiple Keys in Python – Be on the Right Side of Change
February 23, 2024 - One of the most flexible approaches to sort a list of tuples is using the sorted() function and specifying a custom sorting key via a lambda function that returns a tuple representing the multiple sorting keys. Python sorts based on the order of elements within the key tuple.
🌐
30 Seconds of Code
30secondsofcode.org › home › dictionary › sort dictionary list using a tuple key
Sort Python dictionary list using a tuple key - 30 seconds of code
January 4, 2023 - friends = [ {"name": "John", "surname": "Doe", "age": 26}, {"name": "Jane", "surname": "Doe", "age": 28}, {"name": "Adam", "surname": "Smith", "age": 30}, {"name": "Michael", "surname": "Jones", "age": 28} ] print( sorted( friends, key=lambda friend: (friend["age"], friend["surname"], friend["name"]) ) ) # PRINTS: # [ # {'name': 'John', 'surname': 'Doe', 'age': 26}, # {'name': 'Jane', 'surname': 'Doe', 'age': 28}, # {'name': 'Michael', 'surname': 'Jones', 'age': 28}, # {'name': 'Adam', 'surname': 'Smith', 'age': 30} # ] ... An article collection of list helpers and tips for Python 3.6.
🌐
Python Forum
python-forum.io › thread-16928.html
sort lists of lists with multiple criteria: similar values need to be treated equal
Hi, I want to sort a list of lists according to multiple criteria, in a way that similar values are treated as equals. Let's get into details. There is a list that contains up to 12 lists with values (x, y, r). circles = [[536, 565, 326], [2132, ...
🌐
Poggregate
poggregate.com › spaces › python › posts › python-3-sorting-a-list-of-objects-or-dictionaries-by-multiple-keys-bidirectionally
Python 3: Sorting a List of Objects or Dictionaries by Multiple Keys Bidirectionally - Python | Poggregate
October 19, 2021 - # Sort a list of dictionary objects ... for sorting lists containing dictionaries or objects. sorted also allows your key function to return a tuple of values if you wish to sort your list by more than one key like so: # Sort by multiple keys - case sensitive from operator ...
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › python-sorted
Python Sorted: Python Sorted Function, Python Sorted descending
October 19, 2021 - In Python, to use the sorted() function with two key values you need to create a lambda function first which takes an element as a parameter and then returns a 2-tuple of mapped values.
🌐
GitHub
gist.github.com › malero › 418204
python-sort-list-object-dictionary-multiple-key.1.py · GitHub
Use -column to sort in descending order functions -- A Dictionary of Column Name -> Functions to normalize or process each column value getter -- Default "getter" if column function does not exist operator.itemgetter for Dictionaries ...