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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:26:21+00:00 2026-06-12T18:26:21+00:00

There is a list: a = [(ax, 1), (ec, 3), (bk, 5)] another list:

  • 0

There is a list:

a = [("ax", 1), ("ec", 3), ("bk", 5)]

another list:

b = ["ec", "ax", "bk"]

I want to sort a according to b:

sort_it(a, b)

a = [("ec", 3), ("ax", 1), ("bk", 5)]

How to do this?

  • 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-12T18:26:22+00:00Added an answer on June 12, 2026 at 6:26 pm
    a.sort(key=lambda x: b.index(x[0]))
    

    This sorts a in-place using the the index in b of the first element of each tuple from a as the values it sorts on.

    Another, possibly cleaner, way of writing it would be:

    a.sort(key=lambda (x,y): b.index(x))
    

    If you had large numbers of items, it might be more efficient to do things a bit differently, because .index() can be an expensive operation on a long list, and you don’t actually need to do a full sorting since you already know the order:

    mapping = dict(a)
    a[:] = [(x,mapping[x]) for x in b]
    

    Note that this will only work for a list of 2-tuples. If you want it to work for arbitrary-length tuples, you’d need to modify it slightly:

    mapping = dict((x[0], x[1:]) for x in a)
    a[:] = [(x,) + mapping[x] for x in b]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a List or Array of some sort, storing this information
Is there any builtins to check if a list is contained inside another list
Why is there no list-style infinite type error when I define something like this
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
There is list <nodes> <node attr='1'/> <node attr='0'/> <node attr='1'/> <node attr='1'/> </nodes> i
Is there a list of items on the web that you should not use
Is there a list of them with examples accessible to a person without extensive
Is there a list of reserved memory addresses out there - a list of
Is there a list of the differences between the various versions of Rails somewhere
Is there a list of COM error codes somewhere? Related: HRESULT: 0x80010105 (RPC_E_SERVERFAULT) question

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.