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 7693209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:01:56+00:00 2026-05-31T21:01:56+00:00

Given a list of dictionaries like this: x = [ {‘name’:’a’, ‘student’: 1 ,

  • 0

Given a list of dictionaries like this:

x = [
        {'name':'a', 'student': 1 , 'age':19}, 
        {'name':'b', 'student': 0 , 'age':10}
    ]

I want to sort it by age only if student is equal to 1. Can I somehow put that if in the following statement?

sortedlist = sorted(x, key=lambda k: k['age'])

Thanks,

  • 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-05-31T21:01:58+00:00Added an answer on May 31, 2026 at 9:01 pm

    If you use itemgetter + a generator, instead of a lambda + list comp, you get the best performance I have found so far. This was tested on a dicts list of 10k elements. Almost a 30% speed increase over list comp + lambda. Also, if you can safely assume ‘student’ is always a valid key and access it directly, you again gain more speed over having to use d.get('student', 0) == 1

    from operator import itemgetter
    
    sorted((d for d in x if d['student']==1), key=itemgetter('age'))
    
    • Note about lambda vs itemgetter: The reason itemgetter is faster (and I am mostly sure about this) is because the lookup is done on the C side of code. Whereas when you use a lambda you are doing it on the python side which is slower.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list like this: num = [1, 2, 3, 4, 5] There are
I have a list of dictionaries like so: listDict = [{'id':1,'other':2},{'id':3,'other':4},{'id':5,'other':6}] I want a
How do I sort a list of dictionaries by a specific key's value? Given:
Given a list of dictionaries ( each of which have same keys), I want
Given a list of elements like so: int[] ia = new int[] { -4,
Given a list of objects with a date and decimal, I would like to
Given the following list that contains some duplicate and some unique dictionaries, what is
I am given a list of months ( TOTAL_NUM_MONTHS ) and from this list
I want to make prolog returning me all possible sublists of given list so
Trying to remove a userid from a given list. Can't seem to crack it...

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.