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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:41:32+00:00 2026-05-29T05:41:32+00:00

In Python, I have something like the following (although randomly shuffled): l = [(‘a’,

  • 0

In Python, I have something like the following (although randomly shuffled):

l = [('a', 'x'),
     ('a', 'y'),
     ('a', 'z'),
     ('b', 'x'),
     ('b', 'y'),
     ('b', 'z'),
    ]

If I call sorted(l), I get a sorted result (like above) which is what one would expect. However, what I need is to forward sort on the first element of the tuple and reverse sort on the second element. In other words, I would like the following result:

l = [('a', 'z'),
     ('a', 'y'),
     ('a', 'x'),
     ('b', 'z'),
     ('b', 'y'),
     ('b', 'x'),
    ]

In Python2.x, there exists a cmp parameter that can be passed to sorted() to achieve this result, but Python3 no longer has this. It has only a key parameter. Is there any way to achieve the desired sort order using only the key parameter?

I know I can define a whole new class to wrap my tuples, or use something like functools.cmp_to_key (which also creates a wrapper class), but that all seems heavy handed for such a simple operation. Is there no other recourse?

edit: I should add that the strings won’t all be one-character strings and that, in some cases, the list of tuples contain non-string data [i.e. (basestring, datetime)].

  • 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-29T05:41:34+00:00Added an answer on May 29, 2026 at 5:41 am

    Sorting in Python is stable as of Python 2.2.

    So, you can sort by the second value first with the reverse flag on:

    >>> from operator import itemgetter
    >>> l.sort(key=itemgetter(1), reverse=True)
    >>> l
    [('a', 'z'), ('b', 'z'), ('a', 'y'), ('b', 'y'), ('a', 'x'), ('b', 'x')]
    

    Then you can sort by the first value:

    >>> l.sort(key=itemgetter(0))
    >>> l
    [('a', 'z'), ('a', 'y'), ('a', 'x'), ('b', 'z'), ('b', 'y'), ('b', 'x')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have string like combination.py , I want to get the following result
How do I make something like user-defined __repr__ in Python? Let's say I have
i have something similar to the following... used a python script to JSON to
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
Does Python have extension methods like C#? Is it possible to call a method
I am a beginner with Python and trying few programs. I have something like
If I have something like the following class class Foo { private: int _bar;
I have something roughly like the following. Basically I need to access the class
I am trying to do something like the following in python, but i am
Say I have something like the following: dest = \n.join( [line for line in

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.