๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ the python coding stack โ€บ the key to the key parameter in python
The Key To The key Parameter in Python
June 25, 2024 - The built-in function sum() adds these values to return 6. This is the value used by max() to determine the "largest" number using the rule defined by the key parameter. Therefore, max() returns the number with the largest sum of its digits. ... I'm getting closer to the official launch of The Python Coding Place (15 January 2024).
๐ŸŒ
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 - In Python, you can specify a function or callable for the key argument in the built-in functions such as sorted(), max(), min(), etc. Sorting HOW TO - Key Functions โ€” Python 3.11.4 documentation Buil ...
๐ŸŒ
YouTube
youtube.com โ€บ noah rubin
key Function // Python One-Liner Tips & Tricks - YouTube
In this video, we learn how the key function parameter of the sorted, min, and max functions works.
Published: March 15, 2019
Views: 2K
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ dictionary โ€บ keys
Python Dictionary keys() (With Examples)
Built-in Functions ยท List Methods ... popitem() Python Dictionary items() Python Set update() The keys() method extracts the keys of the dictionary and returns the list of keys as a view object....
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python dictionary keys() method
Python Dictionary Keys() Method - Scaler Topics
March 16, 2022 - Learn about Python Dictionary Keys by Scaler Topics. The Python keys() method is used to retrieve all of the keys from the dictionary.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_dictionary_keys.asp
Python Dictionary keys() Method
Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ what is python dictionary keys() method?
What is Python Dictionary keys() Method? - Analytics Vidhya
January 31, 2024 - Also Read: Working with Lists & Dictionaries in Python ยท The keys() method is a built-in function in Python dictionaries that returns a view object containing all the keys.
Find elsewhere
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-dictionary-keys-function
Python Dictionary keys
September 28, 2018 - The Python dictionary keys function is used to return a view object that contains the list of available or total keys in a dictionary as dict_keys object.
๐ŸŒ
CodeReindeer
codereindeer.com โ€บ en โ€บ python-key-function
Python Key Function | CodeReindeer
February 2, 2025 - The key function in Python is an optional argument used in many built-in functions like sorted(), min(), max(), and bisect_left() to customize how elements are compared.
๐ŸŒ
Toppr
toppr.com โ€บ guides โ€บ python-guide โ€บ references โ€บ methods-and-functions โ€บ methods โ€บ dictionary โ€บ keys โ€บ pthon-dictionary-keys
Python Dictionary keys() function | Why do we use the Python keys()? |
September 20, 2021 - Python dictionary keys() function is used to return a new view object that contains a list of all the keys in the dictionary. The Python dictionary keys() method returns an object that contains all the keys in a dictionary.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-dictionary-keys-method
Python Dictionary keys() method - GeeksforGeeks
June 3, 2026 - dict.keys() method in Python returns a view object that contains all the keys of the dictionary. The returned object is dynamic, meaning any changes made to the dictionary are automatically reflected in the view.
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python dictionary keys: a complete guide
Python Dictionary Keys: A Complete Guide: A Complete Guide | Career Karma
December 1, 2023 - The Python dictionary keys() method returns all the keys in a dictionary. This method returns keys as a special object over which you can iterate.
๐ŸŒ
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.

๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ dictionary_keys.htm
Python dictionary keys() Method
A list of all the keys related to the Python dictionary is contained in the view object that the keys() method returns.
๐ŸŒ
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?

๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ sorting.html
Sorting Techniques โ€” Python 3.14.8 documentation
The value of the key parameter should be a function (or other callable) that takes a single argument and returns a key to use for sorting purposes.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-use-key-functions-in-python-sorting-450853
How to use key functions in Python sorting | LabEx
This tutorial explores how to leverage key functions to sort complex data structures efficiently, offering practical techniques that enhance code flexibility and performance in various programming scenarios. In Python, sorting is a fundamental operation that can be enhanced using key functions.