First, you have to put names in quotes, otherwise it's illegal in python.

Then maybe this will make it easier to understand:

>>> employees = [
...     {'Name': 'Allan', 'Age': 25, 'Salary': 10000},
...     {'Name': 'Sharon', 'Age': 30, 'Salary': 8000},
...     {'Name': 'John', 'Age': 18, 'Salary': 1000} 
... ]
>>> 
>>> sorted( employees, key = lambda x : x['Name'] )
[
    {'Salary': 10000, 'Age': 25, 'Name': 'Allan'},
    {'Salary': 1000, 'Age': 18, 'Name': 'John'},
    {'Salary': 8000, 'Age': 30, 'Name': 'Sharon'}
]

Make note, I don't use any external function, just the lambda, which has no name, just a single parameter (x in our case), that's one of the items on your list, and it returns the key according to which the list will be sorted. You may try to return x['Salary'] or x['Age'] and see the result. The name get_name is quite accidental and has absolutely no influence. Things are actually much easier with lambdas.

Answer from lenik on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ sorting.html
Sorting Techniques โ€” Python 3.14.6 documentation
The key function patterns shown above are very common, so Python provides convenience functions to make accessor functions easier and faster. The operator module has itemgetter(), attrgetter(), and a methodcaller() function. Using those functions, the above examples become simpler and faster: >>> from operator import itemgetter, attrgetter >>> sorted(student_tuples, key=itemgetter(2)) [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] >>> sorted(student_objects, key=attrgetter('age')) [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what is the use of the `key` argument in the sort() function?
r/learnpython on Reddit: What is the use of the `key` argument in the sort() function?
June 26, 2022 -

Hi there, I was learning about the lambda/anonymous functions. And I came across this example which sorts a list:

lst = ["Mouse Keyboard", "Router Hub", "CPU UPS"]
lst.sort(key = lambda each: each.split(" ")[-1].lower())
print(lst)

# Which returns output:-
# ['Router Hub', 'Mouse Keyboard', 'CPU UPS']

I tried to google this but was not successful enough. If someone kindly enlightens me on what the key in the sort() function is for. With the google search, I concluded that it takes a function as input. So, the input here will be the 2nd word of each item in the list. But I'm unable to figure out how the sorting function is performed.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-sorted-function
Python sorted() Function - GeeksforGeeks
December 20, 2025 - Explanation: key=lambda x: x['score'] specifies that the sorting should be done using the 'score' value from each dictionary ยท Python Sort() Method ยท Sort Python Dictionaries By Key or Value ยท How to Sort a Set of Values?
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-sort-python-dictionaries-by-key-or-value
Sort Python Dictionary by Key or Value - Python - GeeksforGeeks
Given a dictionary in Python, the task is to sort it by key or value, either in ascending or descending order.
Published ย  January 13, 2026
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python sorting
Python Sorting | Python Education | Google for Developers
The sorted() function can accept a reverse=True argument to sort in descending order. Custom sorting can be achieved using the key= argument with sorted(), specifying a function to determine the sorting value for each element.
๐ŸŒ
Medium
johngrant.medium.com โ€บ python-list-sorting-keys-lambdas-1903b2a4c949
Python โ€” List Sorting, Keys & Lambdas | by John Grant | Medium
July 22, 2016 - โ€œboth list.sort() and sorted() ... prior to making comparisons. The value of the key parameter should be a function that takes a single argument and returns a key to use for sorting purposes....
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_list_sort.asp
Python List sort() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... The sort() method sorts the list ascending by default. You can also make a function to decide the sorting criteria(s). ... # A function that returns the length of the value: def myFunc(e): return len(e) cars = ['Ford', 'Mitsubishi', 'BMW', 'VW'] cars.sort(key=myFunc) Try it Yourself ยป
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ list-sort()
Python List sort(): Master Data Sorting | Learn Now
Use reverse=True for Descending Order: To sort from highest to lowest, pass the reverse=True argument to the method. Use key for Custom Logic: The key parameter accepts a function (often a lambda) that tells .sort() what to sort by.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ sorted
Python sorted()
The sorted() method sorts the elements of the given iterable in ascending order and returns it. In this tutorial, we will learn about the Python sorted() function with the help of examples.
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ how-to-sort-a-dictionary-by-key-or-value-in-python
How to Sort a Dictionary by Key or Value in Python | Codecademy
When applied to a dictionaryโ€™s items, it enables sorting by values in ascending or descending order. The syntax for the sorted() function is: ... key (optional): A function that defines the sorting criterion.
๐ŸŒ
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.
๐ŸŒ
Ducat
ducatindia.com โ€บ blog โ€บ difference-between-sort-and-sorted-in-python
What is the Difference Between sort() and sorted() in Python
Best Training Institute in Noida
Example1: Using sorted() function with List in Python ... The key difference between sort() and sorted() is their return value. Sort () returns nothing because it changes the original list in place. This institute is very nice. I am a student of MERN Full Stack. Nitin Sir is an excellent Trainer, and Nitin Sir is also an excellent Trainer for frontend. The Placement team is also very supportive in helping students get better job opportunities
Rating: 5 โ€‹
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-sort-dictionary-by-key
Python Sort Dictionary by Key โ€“ How to Sort a Dict with Keys
May 25, 2023 - The key must be unique within the dictionary, meaning that no two keys can have the same value. The simplest way to sort a dictionary by its keys is by using the sorted() function along with the items() method of the dictionary.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.6 documentation
Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order (if you want it sorted, just use sorted(d) instead).
๐ŸŒ
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 - The function specified for the key argument is used only for comparison; the actual elements in the result remain unchanged. If you want to modify list elements directly using a function, consider employing list comprehensions. ... The same applies to the sort() method of lists. Sort a list, string, tuple in Python (sort, sorted)
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ python list sort() method | all use cases (+code examples)
Python List sort() Method | All Use Cases (+Code Examples)
February 3, 2025 - The Python list sort() method sorts lists in place using the key and reverse parameters, enabling flexible ordering and custom sorting for various scenarios.
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ how-to-sort-a-list-in-python
How to sort a list in Python | Codecademy
Python offers two built-in sorting methods: .sort() (modifies the original list) and sorted() (returns a new sorted list). The key parameter allows customization of sorting behavior using lambda functions, named functions, or the operator module.