๐ŸŒ
W3Schools
w3schools.com โ€บ Python โ€บ ref_func_sorted.asp
Python sorted() Function
Python Examples Python Compiler ... Study Plan Python Interview Q&A Python Training ... The sorted() function returns a sorted list of the specified iterable object....
Discussions

Sorting lists in python: sorted() vs sort()
sorted() is functional and .sort() is an instance method. Functions should preferably not modify input parameters while object method would be expected to modify act on the instance. Edit: I know that my statement doesn't hold true in all instances, but when making your own functions and methods it's a good way to implement it like described. Documentation is key as always. More on reddit.com
๐ŸŒ r/Python
32
904
May 16, 2022
Can someone explain the `key=` argument for the sorted function
Python Sorted() Docs The "key" argument accepts a function that the sorted algorithm will apply to each item and uses the function's output as the basis of the sort. A common function passed to key is str.lower, which converts the string to lowercase before sorting to prevent things like a capital Z coming before a lowercase a. In your example, for the key, each string is itself being "sorted", which would result in the number being the first value of your string. In [182]: sorted('T4est') Out[182]: ['4', 'T', 'e', 's', 't'] # T comes first because capital More on reddit.com
๐ŸŒ r/learnpython
8
3
June 27, 2025
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-sorted-function
Python sorted() Function - GeeksforGeeks
December 20, 2025 - sorted() function in Python returns a new sorted list from the elements of any iterable, such as a list, tuple, set, or string.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-sort.html
Python Sorting
>>> sorted(strs, reverse=True) ['zebra', 'donut', 'banana', 'apple'] By default in Python, uppercase chars come before lowercase chars, so uppercase strings will sort to the front of the list:
๐ŸŒ
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 Training ... The sort() method sorts the list ascending by default.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ sorted
Python sorted()
Online Python Online JavaScript ... Online Rust Online Scala Online Dart Online R Online Ruby ... The sorted() method sorts the elements of the given iterable in ascending order and returns it....
Find elsewhere
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ builtin-functions โ€บ sorted
sorted() | Pythonโ€™s Built-in Functions โ€“ Real Python
The built-in sorted() function returns a new sorted list from the elements of any iterable passed to it.
๐ŸŒ
YouTube
youtube.com โ€บ watch
MASTERING Python's SORTED Function is Easier Than You Think! - YouTube
๐Ÿš€ Think Python's sorted() function is complicated? Think again! In this comprehensive tutorial, I'll show you exactly how to master this powerful function w...
Published: January 2, 2025
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python sorting
Python Sorting | Python Education | Google for Developers
Custom sorting can be achieved using the key= argument with sorted(), specifying a function to determine the sorting value for each element.
๐ŸŒ
Hyperskill
hyperskill.org โ€บ university โ€บ python โ€บ sorting-and-sort-in-python
Python sort() and sorted(): Sort Lists, Strings & Dicts
June 5, 2026 - The Python sort() function is used to sort elements in a list. By default, it arranges elements in ascending order and modifies the original list in-place.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ can someone explain the `key=` argument for the sorted function
r/learnpython on Reddit: Can someone explain the `key=` argument for the sorted function
June 27, 2025 -

Hi,

So I was doing a code challenge and it's about sorting a string in numerical order based on the integer as part of the string, e.g:

"is2 Thi1s T4est 3a"  -->  "Thi1s is2 3a T4est"

I did it by creating a list with placeholder values and then assigned the values based on the number identified, see:

def order(sentence):
  temp = sentence.split()
  result = [0 for x in range(len(temp))]

  for item in temp:
    for char in item:
      if char.isnumeric():
        num = int(char)
        result[num-1] = item

  return " ".join(result)

I was just looking at other solutions and saw this cool one liner:

return sorted(temp, key=lambda w:sorted(w))

But I don't quite understand how it works :(

I have used the key= argument in the past, for example sorting by the size of the string, i.e key=len

The lambda uses a variable, w and passes it through sorted, but how does that sort by the number included in the string?

๐ŸŒ
Processing
py.processing.org โ€บ reference โ€บ sorted
sorted() \ Language (API)
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
๐ŸŒ
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 - sort vs sorted python - Looking for Difference Between sort and sorted in Python then read here about sort python function and sorted python function to get the difference. 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 โ€‹
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ HowTo โ€บ Sorting
HowTo/Sorting
Maintaining order using a specialized data structure can avoid very slow behavior (quadratic run-time) in the naive approach of editing and constantly re-sorting. Several implementations are described here. Python SortedContainers Module - Pure-Python implementation that is fast-as-C implementations.
๐ŸŒ
Basic Python
astuntechnology.github.io โ€บ python-basics โ€บ sorting.html
Python Sorting | Basic Python
The easiest way to sort is with the sorted(list) function, which takes a list and returns a new list with those elements in sorted order.
Top answer
1 of 4
14

The function you pass in to key is given each of the items that are being sorted, and returns a "key" that Python can sort by. So, if you want to sort a list of strings by the reverse of the string, you could do this:

list_of_strings.sort(key=lambda s: s[::-1])

This lets you specify the value each item is sorted by, without having to change the item. That way, you don't have to build a list of reversed strings, sort that, then reverse them back.

# DON'T do this

data = ['abc', 'def', 'ghi', 'jkl']
reversed_data = [s[::-1] for s in data]
reversed_data.sort()
data = [s[::-1] for s in reversed_data]

# Do this

data.sort(key=lambda s: s[::-1])

In your case, the code is sorting each item by the second item in the tuple, whereas normally it would initially sort by the first item in the tuple, then break ties with the second item.

2 of 4
9
>>> votes = {'Charlie': 20, 'Able': 10, 'Baker': 20, 'Dog': 15}

If we apply .items() on the votes dictionary above we get:

>>> votes_items=votes.items()
>>> votes_items
[('Charlie', 20), ('Baker', 20), ('Able', 10), ('Dog', 15)]
#a list of tuples, each tuple having two items indexed 0 and 1

For each tuple, the first index [0] are the strings ('Charlie','Able','Baker','Dog') and the second index [1] the integers (20,10,20,15).

print(sorted(votes.items(), key = lambda x: x[1])) instructs python to sort the items(tuples) in votes using the second index [1] of each tuple, the integers, as the basis of the sorting.

Python compares each integer from each tuple and returns a list that has ranked each tuple in ascending order (this can be reversed with the reverse=True argument) using each tuple's integer as the key to determine the tuple's rank,

Where there is a tie in the key, the items are ranked in the order they are originally in the dictionary. (so ('Charlie', 20) is before ('Baker', 20) because there is a 20==20 tie on the key but ('Charlie', 20) comes before ('Baker', 20) in the original votes dictionary).

The output then is:

 [('Able', 10), ('Dog', 15), ('Charlie', 20), ('Baker', 20)]

I hope this makes it easier to understand.