How to merge two lists in Python

Merging lists in Python combines multiple sequences into a unified collection. Python offers several built-in methods and operators like extend(), +, and list comprehension to efficiently merge lists while maintaining data integrity.

This guide covers essential merging techniques, optimization strategies, and real-world applications, with code examples created using Claude, an AI assistant built by Anthropic.

Using the + operator

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

The + operator creates a new list by concatenating the elements from both input lists in sequence. This approach maintains the original order of elements while keeping the source lists unchanged—a crucial feature when you need to preserve your input data.

While straightforward, the + operator's memory usage scales with the size of both lists since it creates a completely new list in memory. For small to medium-sized lists, this rarely impacts performance. However, when working with very large datasets, methods like extend() often prove more memory-efficient.

Basic list merging techniques

Beyond the + operator, Python offers three powerful list merging approaches—extend(), itertools.chain(), and list comprehension—each optimized for specific use cases.

Using the extend() method

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

The extend() method modifies the original list by appending all elements from another list. Unlike the + operator, it doesn't create a new list in memory. Instead, it directly adds elements to the end of the existing list.

  • The method efficiently handles large datasets since it requires less memory overhead
  • After calling extend(), the original list (list1) permanently changes to include all elements from list2
  • The second list (list2) remains unchanged throughout the process

This approach particularly shines when you need to combine lists while minimizing memory usage. However, consider using the + operator if you need to preserve the original list's contents.

Using itertools.chain()

import itertools
list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = list(itertools.chain(list1, list2))
print(merged_list)
[1, 2, 3, 4, 5, 6]

The itertools.chain() function creates an iterator that efficiently combines multiple sequences without loading the entire dataset into memory at once. This memory-efficient approach makes it particularly useful when working with large lists.

  • The function accepts any number of iterables as arguments, not just two lists
  • Converting the iterator to a list using list() creates the final merged sequence
  • Both input lists remain unchanged throughout the process

While itertools.chain() requires an extra import statement, it offers significant performance benefits for memory-intensive operations. The function's ability to work with multiple iterables makes it more versatile than basic list concatenation methods.

Using list comprehension

list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = [item for sublist in [list1, list2] for item in sublist]
print(merged_list)
[1, 2, 3, 4, 5, 6]

List comprehension offers a concise, readable way to merge lists in a single line of code. The syntax [item for sublist in [list1, list2] for item in sublist] creates a new list by iterating through each element of both input lists.

  • The outer loop for sublist in [list1, list2] processes each source list
  • The inner loop for item in sublist extracts individual elements
  • Python combines these elements into the final merged list while maintaining their original order

This approach particularly shines when you need clean, expressive code that clearly shows your intent. It creates a new list in memory similar to the + operator. Consider using extend() instead for very large lists where memory efficiency matters more than syntax elegance.

Advanced list merging techniques

Beyond the foundational merging techniques, Python offers specialized tools like the * operator, duplicate handling, and NumPy arrays to handle complex list operations with greater control and performance.

Using the unpacking operator *

list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = [*list1, *list2]
print(merged_list)
[1, 2, 3, 4, 5, 6]

The unpacking operator * elegantly expands each list's elements into a new list. When you write [*list1, *list2], Python unpacks all elements from both lists into the square brackets, creating a merged list.

  • The * operator works with any iterable object. You can unpack multiple lists, tuples, or sets in a single line
  • This syntax creates a new list in memory. The original lists remain unchanged
  • The order of elements follows the sequence you specify in the square brackets

This modern approach offers cleaner syntax compared to traditional concatenation methods. It's especially useful when you need to merge multiple sequences in a readable way.

Merging lists while removing duplicates

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
merged_list = list(dict.fromkeys(list1 + list2))
print(merged_list)
[1, 2, 3, 4, 5, 6]

This technique combines list merging with duplicate removal in a single operation. The dict.fromkeys() method creates a dictionary using list elements as keys, automatically eliminating duplicates since dictionary keys must be unique. Converting the result back to a list preserves the original order while keeping only unique values.

  • The + operator first combines both lists: [1, 2, 3, 4, 3, 4, 5, 6]
  • dict.fromkeys() transforms this into a dictionary with unique keys
  • The final list() conversion produces [1, 2, 3, 4, 5, 6]

This approach particularly shines when working with datasets that might contain overlapping values. It maintains clean, readable code while efficiently handling duplicate removal in a memory-conscious way.

Using NumPy for high-performance merging

import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
merged_array = np.concatenate((array1, array2))
merged_list = merged_array.tolist()
print(merged_list)
[1, 2, 3, 4, 5, 6]

NumPy's concatenate() function offers a high-performance alternative for merging large numerical arrays. The method works directly with NumPy's optimized array data structure, making it significantly faster than traditional Python list operations for data-intensive tasks.

  • Convert Python lists to NumPy arrays using np.array()
  • Merge arrays with np.concatenate() which expects a tuple of arrays as input
  • Transform the result back to a Python list using tolist() for compatibility with standard Python operations

This approach particularly excels when processing large datasets or performing complex mathematical operations. NumPy's underlying C implementation delivers superior performance compared to Python's built-in list methods.

Combining movie watchlists with the + operator

The + operator elegantly combines two movie watchlists into a unified family collection, making it simple to merge different viewing preferences while preserving everyone's favorite films in their original order.

parent_watchlist = ["Inception", "The Matrix", "Interstellar"]
child_watchlist = ["Frozen", "Toy Story", "The Lion King"]
family_watchlist = parent_watchlist + child_watchlist
print("Family movie night options:")
for movie in family_watchlist:
    print(f"- {movie}")

This code demonstrates list concatenation and iteration to create a movie display system. The + operator combines two separate lists into family_watchlist, preserving the exact order of movies from both collections.

  • The print() statement creates a header for the movie list
  • The for loop processes each movie title sequentially
  • F-strings (f"- {movie}") format each title with a bullet point prefix

When executed, this code outputs a neatly formatted list where each movie appears on its own line, preceded by a dash. The approach efficiently handles any number of movies in either input list while maintaining a clean, readable display format.

Analyzing sales data after merging with the + operator

The + operator enables rapid analysis of combined sales data from multiple stores by merging separate revenue lists into a unified dataset for calculating key metrics like averages and identifying performance trends.

store1_sales = [1200, 1500, 900]
store2_sales = [1000, 1300, 1100]
all_sales = store1_sales + store2_sales
average_sale = sum(all_sales) / len(all_sales)
highest_sale = max(all_sales)
lowest_sale = min(all_sales)
print(f"Average sale: ${average_sale:.2f}")
print(f"Highest sale: ${highest_sale}")
print(f"Lowest sale: ${lowest_sale}")

This code demonstrates efficient sales data analysis using Python's built-in list operations. The + operator combines two separate sales lists into all_sales, creating a unified dataset for analysis. Three key calculations follow: sum() divided by len() computes the average sale amount, while max() and min() identify the highest and lowest values respectively.

  • F-strings format the output with clear labels and dollar signs
  • The :.2f format specifier ensures the average displays exactly two decimal places
  • Each metric prints on a new line for easy reading

This straightforward approach handles numerical analysis without requiring external libraries or complex logic structures.

Common errors and challenges

Python developers frequently encounter three critical challenges when merging lists: distinguishing between similar methods, preventing data corruption, and resolving type compatibility issues.

Fixing confusion between append() and extend()

Developers often misuse append() when they actually need extend(). While extend() adds individual elements from a second list, append() treats the entire second list as a single element. This distinction creates unexpected nested structures.

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.append(list2)
print(list1)

The code creates a nested list structure because append() adds list2 as a single element, resulting in [1, 2, 3, [4, 5, 6]] instead of a flat merged list. Check out the corrected implementation below.

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

The extend() method correctly flattens list2 into list1, adding each element individually. This produces the expected [1, 2, 3, 4, 5, 6] instead of a nested structure. Watch for this common pitfall when combining lists—append() always adds its argument as a single item, even when that item is itself a list.

  • Use extend() when you want to merge all elements from one list into another
  • Choose append() only when you intentionally need nested lists
  • Double-check your method choice when working with list operations to avoid unintended data structures

Avoiding unintended list modifications with .extend()

The extend() method permanently modifies the original list by adding elements from another list. This behavior can cause issues when you need to preserve the original data. The code below demonstrates how extend() alters the source list, making it impossible to revert to its initial state.

original = [1, 2, 3]
additional = [4, 5, 6]
original.extend(additional)
print(original)
print("Original data preserved:", original == [1, 2, 3])

The extend() method directly modifies original, making it impossible to access the initial values later. This permanent change creates data integrity issues in applications that need to reference or reuse the original list. Let's examine the proper solution in the following code.

original = [1, 2, 3]
additional = [4, 5, 6]
merged = original + additional
print(merged)
print("Original data preserved:", original == [1, 2, 3])

The + operator creates a new list while preserving the original data. This approach solves the data integrity issues that extend() can cause. The original lists remain unchanged and accessible for future operations.

  • Always consider whether you need to preserve the original data before choosing a merging method
  • Use + when data preservation matters more than memory efficiency
  • Watch for scenarios where multiple functions might need access to the original list values

This pattern becomes especially important in larger applications where data flows through multiple functions or when implementing features like undo functionality.

Handling type errors when merging with +

The + operator requires matching data types when merging sequences. Python raises a TypeError when you attempt to combine lists with incompatible types like strings or integers. This common issue often surfaces when processing user input or working with mixed data sources.

numbers = [1, 2, 3]
user_input = "4"  # Simulating user input as string
result = numbers + user_input
print(result)

The code fails because Python can't directly combine a list with a string using +. The interpreter expects both operands to be lists. The error message TypeError: can only concatenate list (not "str") to list appears. Check out the corrected implementation below.

numbers = [1, 2, 3]
user_input = "4"  # Simulating user input as string
result = numbers + [int(user_input)]
print(result)

The solution wraps the string input in square brackets and converts it to an integer using int() before concatenation. This creates a single-element list that Python can properly merge with the existing numbers list using the + operator.

  • Watch for type mismatches when merging data from different sources like user inputs, file reads, or API responses
  • Always validate and convert data types before attempting list operations
  • Consider using list comprehension for bulk type conversions when dealing with multiple elements

FAQs

What is the difference between using the plus operator and the extend() method to combine lists?

The + operator creates a new list by copying all elements, while extend() modifies the existing list by adding elements directly. This difference affects both memory usage and performance.

  • Using + requires more memory since it creates a new list object in memory
  • extend() is more efficient for large lists since it avoids copying elements
  • Choose extend() when modifying a list in place. Use + when you need to preserve the original list unchanged

How can I merge lists while removing duplicate elements?

To merge lists and remove duplicates, you can use Python's built-in set data structure, which automatically handles duplicate elimination. Converting your lists to sets with the set() function enables you to combine them using the union() operator | or union() method.

This approach works efficiently because sets store only unique elements and use hash tables internally for fast lookups. Convert the final result back to a list using list() if you need an ordered sequence.

Does concatenating lists with the plus operator modify the original lists?

No, concatenating lists with the + operator doesn't modify the original lists. Instead, it creates a new list containing all elements from both lists in sequence. This behavior aligns with Python's commitment to predictable data handling.

The original lists remain unchanged because list concatenation follows value semantics rather than reference semantics. This approach helps prevent unexpected side effects in your code while giving you explicit control over list modifications.

Can I merge lists of different data types together in Python?

Python lets you merge lists containing different data types using the + operator or extend() method. This flexibility stems from Python's dynamic typing system, which doesn't enforce type consistency within lists.

You can combine integers, strings, and even nested data structures in a single list. However, consider these practical implications:

  • Mixed-type lists can make operations like sorting more complex
  • Type checking becomes more important when processing combined lists
  • Code readability might suffer with overly diverse list contents

Which list merging method is most memory efficient for large lists?

The extend() method offers the most memory-efficient approach for merging large lists in Python. Unlike the + operator which creates a new list object, extend() modifies the existing list in place by adding each element from the source list. This eliminates the need for temporary memory allocation during the merge process.

For example, when merging two lists of 1 million elements each, extend() requires only the memory for the final combined list. The + operator would temporarily need memory for three lists—both original lists plus the new combined one.

🏠