Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:21:58+00:00 2026-06-15T12:21:58+00:00

I saw some examples using built-in functions like sorted , sum etc. that use

  • 0

I saw some examples using built-in functions like sorted, sum etc. that use key=lambda.

What does lambda mean here? How does it work?


For the general computer science concept of a lambda, see What is a lambda (function)?.

See also How are lambdas useful? for some discussion that no longer meets site standards but which you may find useful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:21:59+00:00Added an answer on June 15, 2026 at 12:21 pm

    A lambda is an anonymous function:

    >>> f = lambda: 'foo'
    >>> print(f())
    foo
    

    It is often used in functions such as sorted() that take a callable as a parameter (often the key keyword parameter). You could provide an existing function instead of a lambda there too, as long as it is a callable object.

    Take the sorted() function as an example. It’ll return the given iterable in sorted order:

    >>> sorted(['Some', 'words', 'sort', 'differently'])
    ['Some', 'differently', 'sort', 'words']
    

    but that sorts uppercased words before words that are lowercased. Using the key keyword you can change each entry so it’ll be sorted differently. We could lowercase all the words before sorting, for example:

    >>> def lowercased(word): return word.lower()
    ...
    >>> lowercased('Some')
    'some'
    >>> sorted(['Some', 'words', 'sort', 'differently'], key=lowercased)
    ['differently', 'Some', 'sort', 'words']
    

    We had to create a separate function for that, we could not inline the def lowercased() line into the sorted() expression:

    >>> sorted(['Some', 'words', 'sort', 'differently'], key=def lowercased(word): return word.lower())
      File "<stdin>", line 1
        sorted(['Some', 'words', 'sort', 'differently'], key=def lowercased(word): return word.lower())
                                                               ^
    SyntaxError: invalid syntax
    

    A lambda on the other hand, can be specified directly, inline in the sorted() expression:

     >>> sorted(['Some', 'words', 'sort', 'differently'], key=lambda word: word.lower())
    ['differently', 'Some', 'sort', 'words']
    

    Lambdas are limited to one expression only, the result of which is the return value.

    There are loads of places in the Python library, including built-in functions, that take a callable as keyword or positional argument. There are too many to name here, and they often play a different role.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi im using mysqli and i saw some examples using placeholder like :something and
Does jQuery validation plugin have some dependencies? For example, I saw blogposts that said
I saw some examples of using EF generic reposity. I do not have a
I browsed around and saw that MFMessageComposeViewController doesn't support MMS, however some suggested using
Async CTP is very nice to use. I saw some examples for windows phone,
I saw some websites that (for example): if you want to view your message
I saw some html that displays the results of a PHP code just by
I saw some code like this: try { db.store(mydata); } finally { db.cleanup(); }
I saw some scala code that assign _ to a field of class, what
I already saw some examples about realising a session in ember with a Router

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.