• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Sort Two Lists Together

Using Python to Sort Two Lists Together

June 24, 2022 Leave a Comment

To sort two lists together in Python, and preserve the order of pairs, you can use comprehension, zip() and sorted().

It is a little complicated, as we will explain here shortly, but here is some sample code for how you can sort two lists together using Python.

list1 = [1,4,4,2,3,3]
list2 = [8,7,6,9,9,3]

list1, list2 = (list(x) for x in zip(*sorted(zip(list1,list2), key=lambda pair:pair[0])))

print(list1)
print(list2)

#Output:
[1, 2, 3, 3, 4, 4]
[8, 9, 9, 3, 7, 6]

When working with collections of data, the ability to sort your data based on certain conditions easily can be very valuable.

One such operation is if you want to sort two lists together.

Sorting two lists together means sorting the first list and making sure that the pairwise elements stay in the same order.

To sort two lists together, you can use comprehension, zip() and sorted().

Let’s take this step by step. Below are two lists which we want to sort together.

list1 = [1,4,4,2,3,3]
list2 = [8,7,6,9,9,3]

First, we need to zip the two lists together.

list1 = [1,4,4,2,3,3]
list2 = [8,7,6,9,9,3]

print(list(zip(list1,list2)))

#Output:
[(1, 8), (4, 7), (4, 6), (2, 9), (3, 9), (3, 3)]

Then, we use sorted() with a lambda function passed to the key to sort by the element from the first list.

list1 = [1,4,4,2,3,3]
list2 = [8,7,6,9,9,3]

print(sorted(zip(list1,list2), key=lambda pair:pair[0]))

#Output:
[(1, 8), (2, 9), (3, 9), (3, 3), (4, 7), (4, 6)]

Next, we need to unpack the sorted result with *, and zip the result again to get back the original lists.

list1 = [1,4,4,2,3,3]
list2 = [8,7,6,9,9,3]

print(list(zip(*sorted(zip(list1,list2), key=lambda pair:pair[0]))))

#Output:
[(1, 2, 3, 3, 4, 4), (8, 9, 9, 3, 7, 6)]

Finally, we can use comprehension to get back the original lists and output it to the console.

Below is the full example of how you can sort two lists in Python.

list1 = [1,4,4,2,3,3]
list2 = [8,7,6,9,9,3]

list1, list2 = (list(x) for x in zip(*sorted(zip(list1,list2), key=lambda pair:pair[0])))

print(list1)
print(list2)

#Output:
[1, 2, 3, 3, 4, 4]
[8, 9, 9, 3, 7, 6]

Hopefully this article has been useful for you to learn how to sort two lists together using Python.

Other Articles You'll Also Like:

  • 1.  Get Current Year in Python
  • 2.  Date Format YYYYMMDD in Python
  • 3.  Remove Leading Zeros from String with lstrip() in Python
  • 4.  Write Variable to File Using Python
  • 5.  Perfect Numbers in Python
  • 6.  Using Python to Insert Tab in String
  • 7.  Check if Number is Larger than N in Python
  • 8.  Using Python to Calculate Sum of List of Numbers
  • 9.  Length of Set Python – Get Set Length with Python len() Function
  • 10.  Python not in – Check if Value is Not Included in Object

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy