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

  • Home
  • SEARCH
  • 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 8661389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:29:10+00:00 2026-06-12T16:29:10+00:00

Possible Duplicate: Syntax behind sorted(key=lambda :) I was going through the documentation and came

  • 0

Possible Duplicate:
Syntax behind sorted(key=lambda 🙂

I was going through the documentation and came across this example:

> 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)]

What I don’t understand is what are lambda and student here? Can they be replaced by any other names? And what the : do in student:student[2]? It’s a little ambiguous since I’ve never come across this before.

  • 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-12T16:29:11+00:00Added an answer on June 12, 2026 at 4:29 pm

    Semantically, this:

    print sorted(student_tuples, key=lambda student: student[2])
    

    is the same as this:

    def sort_key(student):
        return student[2]
    
    print sorted(student_tuples, key=sort_key)
    

    lambda just provides an alternative syntax for function definition. The result is a function object, just like the one created by def. However, there are certain things that lambda functions can’t do — like defining new variables. They’re good (depending on who you ask) for creating small one-use functions, such as this one.

    Once you understand that, then all you have to know is that key accepts a function, calls it on every value in the sequence passed to sorted, and sorts the values according to the order that their corresponding key values would take if they were sorted themselves.

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

Sidebar

Related Questions

Possible Duplicate: Javascript syntax: what comma means? I came across the code while reading
Possible Duplicate: What is useful about this C syntax? C variable declarations after function
Possible Duplicate: What does new() mean? Like in title. I wonder what this syntax
Possible Duplicate: What is this weird colon-member syntax in the constructor? I am in
Possible Duplicate: Question on this JavaScript Syntax (“What Does This Do?”) What is the
Possible Duplicate: Transact-SQL shorthand join syntax? I ran across a T-SQL script that does
Possible Duplicate: What does this CSS shorthand font syntax mean? I was looking at
Possible Duplicate: What is this weird colon-member syntax in the constructor? I am looking
Possible Duplicate: Caret in objective C What does this ^ syntax mean in Objective-C?
Possible Duplicate: What's this kind of syntax in PHP? What is the name for

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.