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

  • Home
  • SEARCH
  • 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 7669591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:37:42+00:00 2026-05-31T15:37:42+00:00

Suppose I have: list1 = [3, 2, 4, 1, 1] list2 = [‘three’, ‘two’,

  • 0

Suppose I have:

list1 = [3, 2, 4, 1, 1]
list2 = ['three', 'two', 'four', 'one', 'one2']

Calling list1.sort() will sort it, resulting in [1, 1, 2, 3, 4]. However, can I get list2 to be rearranged in sync with that, to get a result like this?

list1 = [1, 1, 2, 3, 4]
list2 = ['one', 'one2', 'two', 'three', 'four']

Sometimes, people phrase the problem differently: given two lists, they would like to use one to determine the sort order for the other – i.e., sort list2 in the order described by the corresponding values in list1. The trick is that this is equivalent to sorting the "key" values (list1), and then rearranging list2 in the same way. In other words, exactly what is described here. Some answers for the other question, though, discard the "sorted keys" afterwards.

See also: How can I sort a list, according to where its elements appear in another list? – this is another common way that people want to sort one list "based on" another. Before attempting to close duplicate questions, take special care to check exactly what the OP wants. A key clue: do the lists need to be the same length?

  • 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-31T15:37:44+00:00Added an answer on May 31, 2026 at 3:37 pm

    One classic approach to this problem is to use the "decorate, sort, undecorate" idiom, which is especially simple using python’s built-in zip function:

    >>> list1 = [3,2,4,1, 1]
    >>> list2 = ['three', 'two', 'four', 'one', 'one2']
    >>> list1, list2 = zip(*sorted(zip(list1, list2)))
    >>> list1
    (1, 1, 2, 3, 4)
    >>> list2 
    ('one', 'one2', 'two', 'three', 'four')
    

    These of course are no longer lists, but that’s easily remedied, if it matters:

    >>> list1, list2 = (list(t) for t in zip(*sorted(zip(list1, list2))))
    >>> list1
    [1, 1, 2, 3, 4]
    >>> list2
    ['one', 'one2', 'two', 'three', 'four']
    

    It’s worth noting that the above may sacrifice speed for terseness; the in-place version, which takes up 3 lines, is a tad faster on my machine for small lists:

    >>> %timeit zip(*sorted(zip(list1, list2)))
    100000 loops, best of 3: 3.3 us per loop
    >>> %timeit tups = zip(list1, list2); tups.sort(); zip(*tups)
    100000 loops, best of 3: 2.84 us per loop
    

    On the other hand, for larger lists, the one-line version could be faster:

    >>> %timeit zip(*sorted(zip(list1, list2)))
    100 loops, best of 3: 8.09 ms per loop
    >>> %timeit tups = zip(list1, list2); tups.sort(); zip(*tups)
    100 loops, best of 3: 8.51 ms per loop
    

    As Quantum7 points out, JSF’s suggestion is a bit faster still, but it will probably only ever be a little bit faster, because Python uses the very same DSU idiom internally for all key-based sorts. It’s just happening a little closer to the bare metal. (This shows just how well optimized the zip routines are!)

    I think the zip-based approach is more flexible and is a little more readable, so I prefer it.


    Note that when elements of list1 are equal, this approach will end up comparing elements of list2. If elements of list2 don’t support comparison, or don’t produce a boolean when compared (for example, if list2 is a list of NumPy arrays), this will fail, and if elements of list2 are very expensive to compare, it might be better to avoid comparison anyway.

    In that case, you can sort indices as suggested in jfs’s answer, or you can give the sort a key function that avoids comparing elements of list2:

    result1, result2 = zip(*sorted(zip(list1, list2), key=lambda x: x[0]))
    

    Also, the use of zip(*...) as a transpose fails when the input is empty. If your inputs might be empty, you will have to handle that case separately.

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

Sidebar

Related Questions

Suppose I have three lists: list1 = a, c, d, r, t list2 =
Suppose I have two lists of numbers in files f1, f2, each number one
Suppose that we have two List<int> 's List<int> list1 = new List<int> { 1,
First array list:- ArrayList<Object> list1; Second array list:- ArrayList<Object> list2; Suppose I have filled
Suppose I have two lists that holds the list of source file names and
Suppose I have one list: IList<int> originalList = new List<int>(); originalList.add(1); originalList.add(5); originalList.add(10); And
Suppose you have a List of Integers in a meaningful order (List1). You also
Suppose I have one class Person ……… public class Person { public string Name
Suppose I have two lists of type MyCustomType , a class which could be
Suppose I have two lists of strings (list A and list B) with the

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.