5 Best Ways to Merge and Sort Two Lists in Python

Rate this post

πŸ’‘ Problem Formulation: Combining two lists into a single sorted list is a common problem in programming. For example, given two lists, list1 = [5, 3, 2] and list2 = [8, 4, 1], we want to merge them to create one sorted list, [1, 2, 3, 4, 5, 8].

Method 1: Using sorted() and + Operator

This method involves merging two lists by concatenating them using the + operator and then sorting the new list using Python’s built-in sorted() function. This function returns a new list containing all items from the original iterable in ascending order. A simple and efficient way to get a combined sorted list.

Here’s an example:

list1 = [5, 3, 2]
list2 = [8, 4, 1]
merged_list = sorted(list1 + list2)
print(merged_list)

Output:

[1, 2, 3, 4, 5, 8]

This code snippet creates a new list by concatenating list1 and list2. The sorted() function is then called to sort the merged list in ascending order, which is printed as the output.

Method 2: Using itertools.chain()

The itertools.chain() function is used for treating consecutive sequences as a single sequence. This method is useful when you want to merge multiple lists and sort the combined elements, without creating a new list for the merged output, thus potentially saving memory.

Here’s an example:

import itertools

list1 = [5, 3, 2]
list2 = [8, 4, 1]
merged_list = sorted(itertools.chain(list1, list2))
print(merged_list)

Output:

[1, 2, 3, 4, 5, 8]

This code snippet uses itertools.chain() to iterate over both list1 and list2 as if they were a single iterable. The sorted() function is then used to sort the elements and a new sorted list is returned.

Method 3: Using extend() Method

The extend() method is employed to append the contents of one list to the end of another. It modifies the list in place and then you can sort it using the sort() method, which sorts the list in ascending order by default.

Here’s an example:

list1 = [5, 3, 2]
list2 = [8, 4, 1]
list1.extend(list2)
list1.sort()
print(list1)

Output:

[1, 2, 3, 4, 5, 8]

Here, list2 is appended to list1 using the extend() method, and list1.sort() sorts the combined list in place. The result is a sorted merged list.

Method 4: Using heapq.merge()

For large and already sorted lists, the heapq.merge() function is effective because it does not require all of the data to be loaded into memory at once. This creates an iterator over the sorted values, combining the input iterables without creating a new list unless explicitly converted.

Here’s an example:

import heapq

list1 = [2, 3, 5]
list2 = [1, 4, 8]
merged_iterator = heapq.merge(list1, list2)
merged_list = list(merged_iterator)
print(merged_list)

Output:

[1, 2, 3, 4, 5, 8]

This snippet leverages heapq.merge() which efficiently combines list1 and list2 into a sorted iterator. To get a merged list, we cast the iterator to a list.

Bonus One-Liner Method 5: Using List Comprehension and sort()

As a quick one-liner, you can use list comprehension to combine lists and then sort the resulting list with the sort() method.

Here’s an example:

list1 = [5, 3, 2]
list2 = [8, 4, 1]
merged_list = [x for l in (list1, list2) for x in l]
merged_list.sort()
print(merged_list)

Output:

[1, 2, 3, 4, 5, 8]

This code uses a nested list comprehension to flatten the two lists into one, which is then sorted using merged_list.sort().

Summary/Discussion

  • Method 1: Using + Operator and sorted(). Strengths: Simple and concise. Weaknesses: Creates a new list, which uses more memory.
  • Method 2: Using itertools.chain(). Strengths: Can handle multiple lists efficiently, good for memory usage. Weaknesses: Slightly more complex syntax.
  • Method 3: Using extend() Method. Strengths: In-place sorting without creating new list. Weaknesses: Modifies one of the original lists.
  • Method 4: Using heapq.merge(). Strengths: Efficient for large, sorted lists; doesn’t need all data in memory. Weaknesses: Outputs an iterator instead of a list.
  • Method 5: One-liner List Comprehension. Strengths: Quick and elegant. Weaknesses: May be less readable for beginners.