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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:07:36+00:00 2026-05-21T09:07:36+00:00

I am unfortunately having to use python with version before 2.4 So I don’t

  • 0

I am unfortunately having to use python with version before 2.4 So I don’t have the sorted built-in function. I need to sort a list; I found it is not possibly to go with

for aName in mylist.sort(lambda x,y:cmp(x.getName,y.getName)):

because then I would get an error saying

TypeError: iteration over non-sequence

I want to do the following:

for aName in sorted(mylist,key=lambda x:x.getName):

Can anybody help me with this? Many 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-21T09:07:36+00:00Added an answer on May 21, 2026 at 9:07 am

    You said that you wanted to do for aName in sorted(mylist,key=lambda x:x.getName): but accepted an answer that did something else …

    It’s quite possible to write code that will run on multiple versions of Python 2.x. Here’s how to retrofit sorted() functionality to Python 2.1 to 2.3. It uses the DSU (decorate-sort-undecorate) aka “Schwartzian Transform” method … see this section of the Sorting HOWTO but do read the whole HOWTO; it’s very informative.

    try:
        sorted
        def mysorted(iterable, key, reverse=0):
            return sorted(iterable, key=key, reverse=reverse)
    except NameError: # doesn't have "sorted"
        def mysorted(iterable, key, reverse=0):
            temp = [(key(x), x) for x in iterable]
            temp.sort()
            if reverse:
                return [temp[i][1] for i in xrange(len(temp) - 1, -1, -1)]
            return [t[1] for t in temp]
    
    mylist = 'tom dick harriet alfred zechariah'.split()
    mykey = lambda x: x[2:] # ignore 1st 2 characters
    print mylist
    print mysorted(mylist, mykey)
    print mysorted(mylist, mykey, reverse=1)
    

    Running the above script with Python 2.7.1 and 2.1.3 produces the same output:

    ['tom', 'dick', 'harriet', 'alfred', 'zechariah']
    ['zechariah', 'dick', 'alfred', 'tom', 'harriet']
    ['harriet', 'tom', 'alfred', 'dick', 'zechariah']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Unfortunately it looks like for various reasons I'm going to have to use Visual
Unfortunately, I need to do this. I'm using ELMAH for my error log. Before
Unfortunately I don't have access to a *nix box at work or at home.
Unfortunately I have to use Windows Server 2003 on my 32 bit workstation due
The version of Ubuntu I use provides FOP 0.95. Its having problems, so I
Operating system: Windows XP SP3 (Unfortunately) I have downloaded and installed the latest version
We unfortunately find ourselves having to support our product in IE 6 because some
Unfortunately, despite having tried to learn regex at least one time a year for
I am having trouble getting the right hibernate annotations to use on a Map
I've set up my VSTO Excel add-in to use log4net for logging. Unfortunately I'm

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.