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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:00:28+00:00 2026-05-19T03:00:28+00:00

A question was asked here on SO , a few minutes ago, on sorting

  • 0

A question was asked here on SO, a few minutes ago, on sorting dictionary keys based on their values.

I just read about the operator.itemgetter method of sorting a few days back and decided to try that, but it doesn’t seem to be working.

Not that I have any problems with the answers presented to the questions, I just wanted to try this with operator.itemgetter.

So the dict was:

>>> mydict = { 'a1': ['g',6],
           'a2': ['e',2],
           'a3': ['h',3],
           'a4': ['s',2],
           'a5': ['j',9],
           'a6': ['y',7] }

I tried this:

>>> l = sorted(mydict.itervalues(), key=operator.itemgetter(1))
>>> l
[['e', 2], ['s', 2], ['h', 3], ['g', 6], ['y', 7], ['j', 9]]

And this works as I want it to. However, since I don’t have the complete dictionary (mydict.itervalues()), I tried this:

>>> complete = sorted(mydict.iteritems(), key=operator.itemgetter(2))

This doesn’t work (as I expected it to).

So how do I sort the dict using operator.itemgetter and call itemgetter on the nested key – value pair.

  • 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-19T03:00:28+00:00Added an answer on May 19, 2026 at 3:00 am
    In [6]: sorted(mydict.iteritems(), key=lambda (k,v): operator.itemgetter(1)(v))
    Out[6]: 
    [('a2', ['e', 2]),
     ('a4', ['s', 2]),
     ('a3', ['h', 3]),
     ('a1', ['g', 6]),
     ('a6', ['y', 7]),
     ('a5', ['j', 9])]
    

    The key parameter is always a function that is fed one item from the iterable (mydict.iteritems()) at a time. In this case, an item could be something like

    ('a2',['e',2])
    

    So we need a function that can take ('a2',['e',2]) as input and return 2.

    lambda (k,v): ... is an anonymous function which takes one argument — a 2-tuple — and unpacks it into k and v. So when the lambda function is applied to our item, k would be 'a2' and v would be ['e',2].

    lambda (k,v): operator.itemgetter(1)(v) applied to our item thus returns
    operator.itemgetter(1)(['e',2]), which “itemgets” the second item in ['e',2], which is 2.

    Note that lambda (k,v): operator.itemgetter(1)(v) is not a good way to code in Python. As gnibbler points out, operator.itemgetter(1) is recomputed for each item. That’s inefficient. The point of using operator.itemgetter(1) is to create a function that can be applied many times. You don’t want to re-create the function each time. lambda (k,v): v[1] is more readable, and faster:

    In [15]: %timeit sorted(mydict.iteritems(), key=lambda (k,v): v[1])
    100000 loops, best of 3: 7.55 us per loop
    
    In [16]: %timeit sorted(mydict.iteritems(), key=lambda (k,v): operator.itemgetter(1)(v))
    100000 loops, best of 3: 11.2 us per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.