How to merge two lists in Python
Learn to merge two lists in Python. Explore different methods, real-world applications, and tips for debugging common errors.

You often need to merge two lists in Python. The language provides several ways to combine them, from the simple + operator to more advanced methods for different scenarios.
In this article, you'll explore several techniques with practical tips. You'll also find real-world applications and debugging advice to help you select the right method for your project.
Using the + operator
list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = list1 + list2
print(merged_list)--OUTPUT--[1, 2, 3, 4, 5, 6]
The + operator provides the most straightforward way to combine two lists. It creates a new list, merged_list, by taking all the elements from list1 and appending all the elements from list2. The order of the original lists is preserved.
The key takeaway is that this operation is non-destructive, which is a safe and predictable behavior. Specifically:
- It returns a completely new list object.
- The original lists,
list1andlist2, are not modified in any way.
This makes the + operator a great choice when you need to keep your original lists intact for other parts of your program.
Basic list merging techniques
If the + operator doesn't quite fit your needs, Python also provides the extend() method, itertools.chain(), and list comprehensions for more specialized merging tasks.
Using the extend() method
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)--OUTPUT--[1, 2, 3, 4, 5, 6]
Unlike the + operator, the extend() method modifies the list it's called on. In this case, it adds all elements from list2 directly into list1.
- This is an "in-place" operation, meaning
list1is permanently changed. - The method doesn't return a new list; it returns
None. So, you wouldn't assign its result to a new variable.
This makes extend() a good choice when you want to update an existing list and don't need to preserve its original state.
Using itertools.chain()
import itertools
list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = list(itertools.chain(list1, list2))
print(merged_list)--OUTPUT--[1, 2, 3, 4, 5, 6]
For a more memory-efficient approach, you can use itertools.chain(). This function creates a special iterator object that treats the lists as a single sequence without actually creating a new list in memory.
- The function returns an iterator, which is an object that generates items on demand rather than storing them all at once.
- To get a final merged list, you must convert this iterator using the
list()constructor, as shown in the example. - This method is particularly useful when working with very large lists, as it avoids allocating memory for a new list upfront.
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)--OUTPUT--[1, 2, 3, 4, 5, 6]
List comprehensions provide a concise, often more "Pythonic," way to build a new list. This technique essentially flattens a list of lists—in this case, [list1, list2]—into a single new list.
- The expression works by first iterating through the outer list (
for sublist in [list1, list2]). - Then, for each sublist, it iterates through its items (
for item in sublist) and adds them to the new list. - Like the
+operator, this method is non-destructive and returns a new list, leaving the originals untouched.
Advanced list merging techniques
When basic merging isn't enough, you can use the unpacking operator *, filter out duplicates as you combine lists, or tap into specialized libraries for a speed boost.
Using the unpacking operator *
list1 = [1, 2, 3]
list2 = [4, 5, 6]
merged_list = [*list1, *list2]
print(merged_list)--OUTPUT--[1, 2, 3, 4, 5, 6]
The unpacking operator, *, offers a modern and visually clean way to merge lists. When used inside a new list literal, as in [*list1, *list2], it unpacks each list's elements sequentially into the new container. This method is often praised for its readability.
- Like the
+operator, it creates a brand new list, leaving the original lists unchanged. - The syntax is very explicit about what's happening—you're literally placing the contents of the old lists inside a new one.
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)--OUTPUT--[1, 2, 3, 4, 5, 6]
When you need to merge lists and also remove duplicates, you can use a clever trick involving dictionaries. This approach first combines the lists with the + operator, then leverages the fact that dictionary keys must be unique to filter out any repeated elements.
- The
dict.fromkeys()method creates a dictionary from the merged list. Since dictionary keys cannot be duplicates, this step automatically filters them out while preserving the original order. - Finally, wrapping the result in
list()converts these unique dictionary keys back into a new list.
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)--OUTPUT--[1, 2, 3, 4, 5, 6]
When performance is critical, especially with large numerical datasets, the NumPy library offers a powerful solution. It's optimized for fast array operations, making it a go-to for scientific computing and data analysis.
- First, you convert your lists into NumPy arrays using
np.array(). - The
np.concatenate()function then efficiently merges these arrays. - Finally, the
.tolist()method converts the resulting array back into a standard Python list if needed.
Move faster with Replit
Replit is an AI-powered development platform that transforms natural language into working applications. Describe what you want to build, and Replit Agent creates it—complete with databases, APIs, and deployment.
For the list merging techniques we've explored, Replit Agent can turn them into production-ready tools:
- Build a contact management tool that combines customer lists from different spreadsheets, using methods like
+or the unpacking operator*to merge them anddict.fromkeys()to remove duplicates. - Create a log file analyzer that efficiently merges large streams of data from multiple servers using the memory-saving
itertools.chain(). - Deploy a data science dashboard that uses NumPy's
np.concatenate()to quickly combine and process large numerical datasets from various sources.
You can turn any of these concepts into a real application. Describe your idea, and Replit Agent will write the code, handle deployment, and get your project running.
Common errors and challenges
Even with simple operations, you can run into a few common pitfalls when merging lists in Python, from confusing methods to unexpected results.
Fixing confusion between append() and extend()
A frequent mix-up is using the append() method when you actually need extend(). The append() method adds its entire argument as a single new element to the end of a list. If you append one list to another, you'll end up with a nested list, not a merged one.
For example, list1.append(list2) would turn [1, 2, 3] into [1, 2, 3, [4, 5, 6]]. Remember to use extend() when you want to add the individual elements from one list to another, creating a single, flat list.
Avoiding unintended list modifications with .extend()
Because the extend() method modifies a list in place, it doesn't return a new list—it returns None. A common mistake is to assign the result of an extend() operation to a new variable, like merged_list = list1.extend(list2).
This code will leave merged_list with a value of None, which can cause errors later in your program. If you need to create a new merged list without changing the originals, use the + operator or the unpacking operator * instead.
Handling type errors when merging with +
The + operator works only when you're combining two lists. If you try to add a list to another data type, such as a string or an integer, Python will raise a TypeError because it doesn't know how to perform that operation.
This often happens when a variable you thought was a list is actually something else. Before concatenating, always ensure both operands are lists to prevent your program from crashing unexpectedly.
Fixing confusion between append() and extend()
It's easy to confuse append() with extend(), but they behave very differently. Using append() to combine two lists doesn't merge them; instead, it nests the second list inside the first one. The code below shows this common error in action.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.append(list2)
print(list1)
The code adds list2 as a single nested element instead of merging its contents. This structure is often unexpected and can complicate accessing items. The following example shows how to achieve a flat list instead.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)
The extend() method correctly merges the lists by adding each item from list2 individually to list1. This creates the flat list you'd expect, rather than a nested one. That's the key difference from append(). This mix-up is a common pitfall, so always use extend() when your goal is to combine the contents of an iterable into an existing list.
Avoiding unintended list modifications with .extend()
The extend() method is efficient, but it directly alters the list you call it on. This "in-place" modification can be a problem if you need to preserve the original list for later use. It's a permanent change you can't easily undo.
The following code demonstrates how the original list is changed after the operation, which might not be what you intended.
original = [1, 2, 3]
additional = [4, 5, 6]
original.extend(additional)
print(original)
print("Original data preserved:", original == [1, 2, 3])
The final line confirms the problem: the comparison returns False because extend() permanently alters the original list. The following code demonstrates how to merge lists while keeping the original data intact.
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, so your original data remains untouched. As the code shows, merged contains the combined elements, while original is preserved—confirmed when the comparison returns True. This method is the right choice when you need to merge lists but must also keep the original lists intact for other operations. It’s a simple way to avoid unintended changes, or side effects, in your code.
Handling type errors when merging with +
The + operator is strict about what it can combine. It expects two lists, and trying to add a list to another data type—like a string from user input—will cause a TypeError. This error stops your program unexpectedly.
The code below shows what happens when you try to add a string directly to a list, a common pitfall when working with dynamic data.
numbers = [1, 2, 3]
user_input = "4" # Simulating user input as string
result = numbers + user_input
print(result)
The code triggers a TypeError because the + operator cannot concatenate a list, numbers, with a string, user_input. Python requires both operands to be lists. The following example demonstrates how to handle this correctly.
numbers = [1, 2, 3]
user_input = "4" # Simulating user input as string
result = numbers + [int(user_input)]
print(result)
The solution is to ensure both items you're adding are lists. The code fixes the TypeError by first converting the user_input string to an integer with int(). Then, it wraps that integer in square brackets, like [int(user_input)], turning it into a list so the + operator works as expected.
Keep an eye out for this error when handling data from external sources, like user input or API responses, where types aren't always guaranteed.
Real-world applications
Merging lists is a practical skill for common tasks like combining personal collections or processing business data.
Combining movie watchlists with the + operator
You can easily create a combined family movie list from separate parent and child watchlists using the simple + operator.
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 example shows a practical way to aggregate data. It takes two distinct lists, parent_watchlist and child_watchlist, and merges them into a single new list named family_watchlist using the + operator.
- The operation is non-destructive, so the original two lists remain unchanged.
- A
forloop then iterates through the combined list, printing each movie title on its own line.
This is a common pattern for collecting items from multiple sources before presenting them in a unified view.
Analyzing sales data after merging with the + operator
You can also use the + operator to combine sales figures from different stores, making it easy to calculate overall performance metrics like the average, highest, and lowest sales.
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 example shows how merging lists simplifies data analysis. It starts by combining store1_sales and store2_sales into a single all_sales list with the + operator. With all the data in one place, you can use Python's built-in functions for quick calculations.
- The average sale is calculated using
sum()andlen(). - The
max()andmin()functions find the highest and lowest values in the combined list.
The results are then printed using f-strings, where :.2f formats the average as a two-decimal currency value.
Get started with Replit
Now, build something real. Ask Replit Agent to "create a script that merges two contact lists and removes duplicates" or "build a dashboard that combines daily analytics reports."
The agent writes the code, tests for errors, and deploys your application. Start building with Replit.
Create and deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.
Create & deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.



%2520in%2520Python.png)