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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:52:51+00:00 2026-06-16T01:52:51+00:00

For example X=[5,6,2,3,1] Y=[7,2,3,4,6] I sort X: X=[1,2,3,5,6] But I want the same relative

  • 0

For example

X=[5,6,2,3,1]
Y=[7,2,3,4,6]

I sort X:

X=[1,2,3,5,6]

But I want the same relative sort applied to Y so the numbers stay in the same positions relative to each other as before:

Y=[6,3,4,7,2]

I hope this makes sense!

  • 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-16T01:52:53+00:00Added an answer on June 16, 2026 at 1:52 am

    Usually, you do a zip–sort–unzip for this

    >>> X = [5,6,2,3,1]
    >>> Y = [7,2,3,4,6]
    

    Now sort them together:

    >>> sorted(zip(X,Y))
    [(1, 6), (2, 3), (3, 4), (5, 7), (6, 2)]
    

    Pair that with a “unzip” (zip(*...))

    >>> zip(*sorted(zip(X,Y)))
    [(1, 2, 3, 5, 6), (6, 3, 4, 7, 2)]
    

    which you could unpack:

    >>> X,Y = zip(*sorted(zip(X,Y)))
    >>> X
    (1, 2, 3, 5, 6)
    >>> Y
    (6, 3, 4, 7, 2)
    

    Now you have tuple instead of list objects, but if you really need to, you can convert it back.


    As pointed out in the comments, this does introduce a very slight dependence on the second list in the sort: Consider the lists:

    X = [1,1,5,7] #sorted already
    Y = [2,1,4,6] #Not already sorted.
    

    With my “recipe” above, at the end of the day, you’ll get:

    X = (1,1,5,7)
    Y = (1,2,4,6) 
    

    which might be unexpected. To fix that, you could pass a key argument to sorted:

    from operator import itemgetter
    X,Y = zip(*sorted(zip(X,Y),key=itemgetter(0)))
    

    Demo:

    >>> X
    [1, 1, 5, 7]
    >>> Y
    [2, 1, 4, 6]
    >>> XX,YY = zip(*sorted(zip(X,Y)))
    >>> XX
    (1, 1, 5, 7)
    >>> YY
    (1, 2, 4, 6)
    >>> from operator import itemgetter
    >>> XX,YY = zip(*sorted(zip(X,Y),key=itemgetter(0)))
    >>> XX
    (1, 1, 5, 7)
    >>> YY
    (2, 1, 4, 6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to reverse an array (or any other data structure) but because this
I want to sort a list by each item's digit. Example: myCmpItem = '511'
I've used this example to to implement some sort of guestbook, only I'm using
how to sort a list in Scala by two fields, in this example I
For example: int A[] = {3,2,1,2,3,2,1,3,1,2,3}; How to sort this array efficiently? This is
This question is related but not the same as this: How do you give
I have four numbers that can possible be the same, but or most likely
(This is sort of a long-winded question but I have summarized it at the
This might sound a bit complicated, but what I want to do is find
Let's say I want to show multiple Drop Down Boxes each with the same

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.