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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:42:55+00:00 2026-06-14T09:42:55+00:00

order = [‘w’,’x’,’a’,’z’] [(object,’a’),(object,’x’),(object,’z’),(object,’a’),(object,’w’)] How do I sort the above list of tuples by

  • 0
order = ['w','x','a','z']
[(object,'a'),(object,'x'),(object,'z'),(object,'a'),(object,'w')]

How do I sort the above list of tuples by the second element according the the key list provided by ‘order’?

UPDATE on 11/18/13:

I found a much better approach to a variation of this question where the keys are certain to be unique, detailed in this question: Python: using a dict to speed sorting of a list of tuples.

My above question doesn’t quite apply because the give list of tuples has two tuples with the key value of 'a'.

  • 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-06-14T09:42:56+00:00Added an answer on June 14, 2026 at 9:42 am

    You can use sorted, and give as the key a function that returns the index of the second value of each tuple in the order list.

    >>> sorted(mylist,key=lambda x: order.index(x[1]))
    
    [('object', 'w'), ('object', 'x'), ('object', 'a'), ('object', 'a'), ('object', 'z')]
    

    Beware, this fails whenever a value from the tuples is not present within the order list.

    Edit:

    In order to be a little more secure, you could use :

    sorted(mylist,key=lambda x: x[1] in order and order.index(x[1]) or len(order)+1)
    

    This will put all entries with a key that is missing from order list at the end of the resulting list.

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

Sidebar

Related Questions

Order props: Object selectedItems: Array[1] 0: SelectedItem length: 1 __proto__: Array[0] __proto__: Order I
In order to achieve spriting by compass according to the documentation I wrote this:
Order.prototype.selectItem = function(newItem) { ... newItem is the object I want to copy and
Order by is dynamic but the sort order is static. SELECT ... Order By
In order to prepare my 70-513 exam, I found the following question: A Windows
In order to update the shopping cart $ total and item total we are
The Order table has the list of orders as well as a Customer ID
order by in query is taking to much time in MySQL. SHOW PROFILES shows
Order by descending is not working on LINQ to Entity In the following Query
Order deny,allow deny from all allow from xx.xx.x.xx RewriteRule . - [F] I have

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.