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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:07:47+00:00 2026-05-18T01:07:47+00:00

While I was debugging some illogical behavour I came to the following weirdness in

  • 0

While I was debugging some illogical behavour I came to the following weirdness in Python 2.5 sorted() function calls:

>>> aa = [10, 5, 20]
>>> sorted(range(len(aa)))
[0, 1, 2]
sorted(range(len(aa)), key=lambda a: aa[a])
[1, 0, 2]
sorted(range(len(aa)), key=lambda a: -aa[a])
[2, 0, 1]

First two calls work as expected, but the last one is imho simply wrong! It should be: [1, 2, 0].

After further experiments for trying to come to the root of the problem I came to this (does not use lambda or negation operation, but it is otherwise the same problem):

>>> bb = [-10, -5, -20]
>>> sorted([0, 1, 2], key=bb.__getitem__)
[2, 0, 1]

Even things like following don’t work and show that double negation works again:

>>> bb = [-10, -5, -20]
>>> def fun(i):
...    return bb[i]
>>> sorted([0, 1, 2], key=fun)
[2, 0, 1]
>>> def fun2(i):
...     return -bb[i]
>>> sorted([0, 1, 2], key=fun2)
[1, 0, 2]

Am I losing my mind or where is the problem? Or why doesn’t Python 3.x have the cmp argument that used to work fine (compatibility is the reason why I am not using it)?

  • 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-18T01:07:47+00:00Added an answer on May 18, 2026 at 1:07 am

    The value returned by the key function acts as a proxy for the values being sorted.
    So when you say

    sorted(range(len(aa)), key=lambda a: -aa[a])
    

    you are sorting range(len(aa)), that is [0, 1, 2], but using the values
    -aa[0], -aa[1], -aa[2] as the proxy values.

    range(len(aa))   0   1   2    <-- values
    aa[a]           10   5  20
    -aa[a]         -10  -5 -20    <-- proxy values
    

    Since -20, or -aa[2], is the smallest proxy value, its associated value 2
    becomes the first element in the sorted result.

    Since -10, or -aa[0] is the next smallest, its associated value 0 becomes the second element in the sorted result.

    Finally -5, or -aa[1] is the last value, so 1 is the last number in the sorted result.

    Thus, sorted(range(len(aa)), key=lambda a: -aa[a]) equals [2, 0, 1].

    The answer Python is giving is correct.

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

Sidebar

Related Questions

While debugging a classic ASP application (and learning about classic ASP at the same
I do a lot of heavy math programming. Often there is a need to
I am using the debugger to step through my code. The code file I’m
I am trying to do a plain multipart form upload using System.Net.WebClient and Basic
I'm running JBoss as a server started from within Eclipse. I have a strange
I'm at the very beginning of learning ASP.NET WebForms. Right now I'm starting to
I have created structure : struct buffer { string ProjectName ; string ProjectID ;
A situation has come up a few times in the last few weeks when
I am trying to create a very simple application (part of my learning program),
I've been writing and reading PDF's to a SQL Server 2008 FileStream for a

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.