How to Merge and Combine Python Lists the Right Way
Learn the best ways to merge Python lists using +, extend(), * unpacking, itertools.chain, and more. Includes performance tips, deduplication, and real-world examples.
Advertisement
You’ve probably been there. You have two lists, and you just need them to become one. Maybe it’s a list of user names from two different sources, or product IDs from separate databases. In Python, merging lists seems simple — but there are several ways to do it, and each has its own use case. Let’s walk through the options so you can pick the right one for your situation.
The Classic + Operator
The most straightforward way to combine two lists is using the + operator. It creates a new list that contains all elements from the first list followed by all elements from the second.
list_a = [1, 2, 3]
list_b = [4, 5, 6]
merged = list_a + list_b
print(merged) # [1, 2, 3, 4, 5, 6]
This is perfect when you need a fresh copy and don’t want to modify the original lists. It’s also very readable. But if you’re working with large lists, keep in mind that + creates a new list in memory, which can be slower and use more memory than other methods.
Using extend() to Modify in Place
Sometimes you don’t need a new list — you just want to add all items from one list to another. That’s where extend() comes in.
list_a = [1, 2, 3]
list_b = [4, 5, 6]
list_a.extend(list_b)
print(list_a) # [1, 2, 3, 4, 5, 6]
Notice that extend() modifies list_a directly and returns None. This is efficient because it doesn’t create a new list object. Use this when you’re done with the original list and just want to grow it.
The * Unpacking Trick
Python 3.5 introduced a neat way to merge lists using the * unpacking operator inside a list literal.
list_a = [1, 2, 3]
list_b = [4, 5, 6]
merged = [*list_a, *list_b]
print(merged) # [1, 2, 3, 4, 5, 6]
This is my personal favorite for readability. It works with any number of lists, and you can even insert extra elements in between.
merged = [*list_a, 99, *list_b]
# [1, 2, 3, 99, 4, 5, 6]
It’s clean, expressive, and doesn’t require importing anything. The only downside? It creates a new list, so for very large lists, memory usage can be a concern.
When You Need to Remove Duplicates
Sometimes you don’t just want to merge lists — you want to merge them and keep only unique values. A common approach is to convert to a set, but that loses order. If order matters, here’s a trick that preserves it:
list_a = [1, 2, 3, 3]
list_b = [3, 4, 5]
merged_unique = list(dict.fromkeys(list_a + list_b))
print(merged_unique) # [1, 2, 3, 4, 5]
Using dict.fromkeys() keeps the first occurrence of each element and maintains insertion order. This works in Python 3.7+ where dictionaries are ordered. If you’re on an older version, you’d need collections.OrderedDict.
Merging Multiple Lists at Once
What if you have three, four, or even ten lists? You could chain + operators, but that gets messy. Instead, use itertools.chain:
from itertools import chain
list_a = [1, 2]
list_b = [3, 4]
list_c = [5, 6]
merged = list(chain(list_a, list_b, list_c))
print(merged) # [1, 2, 3, 4, 5, 6]
chain() is efficient because it doesn’t create intermediate lists. It just iterates through each input list one after another. This is especially useful when you’re dealing with a large number of lists or very large lists.
The += Shortcut
If you want to modify a list in place, += works just like extend():
list_a = [1, 2, 3]
list_b = [4, 5, 6]
list_a += list_b
print(list_a) # [1, 2, 3, 4, 5, 6]
Under the hood, += calls extend(), so it’s the same operation. Some people find it more readable, especially when the intent is to grow a list.
When Order Doesn’t Matter
If you don’t care about the order of elements and you want to remove duplicates, converting to a set is the fastest approach:
list_a = [3, 1, 2]
list_b = [2, 4, 3]
merged_set = list(set(list_a + list_b))
print(merged_set) # [1, 2, 3, 4] (order may vary)
But remember: sets are unordered. If you need to preserve the original order, stick with the dict.fromkeys() method I showed earlier.
Merging Lists of Lists
What if you have a list of lists and want to flatten it? That’s a different kind of merge. For a shallow flatten, you can use a list comprehension:
list_of_lists = [[1, 2], [3, 4], [5, 6]]
flattened = [item for sublist in list_of_lists for item in sublist]
print(flattened) # [1, 2, 3, 4, 5, 6]
For deeper nesting, itertools.chain.from_iterable() is your friend:
from itertools import chain
flattened = list(chain.from_iterable(list_of_lists))
Performance Considerations
If you’re merging lists in a loop, be careful. Using + inside a loop creates a new list each time, which is O(n²) — very slow for large data. Instead, use extend() or += to build the list incrementally.
# Slow
result = []
for sublist in many_lists:
result = result + sublist # Bad!
# Fast
result = []
for sublist in many_lists:
result.extend(sublist)
The second version reuses the same list object and just appends elements, which is much more efficient.
What About append()?
A common mistake is using append() when you mean extend(). append() adds the entire list as a single element, not its contents.
list_a = [1, 2]
list_b = [3, 4]
list_a.append(list_b)
print(list_a) # [1, 2, [3, 4]]
That’s a nested list, not a merged one. So if you want to combine lists, stick with +, extend(), or the unpacking syntax.
Real-World Example: Combining User Data
At PythonSkillset, we often need to merge user lists from different signup sources. Here’s a practical example:
email_signups = ["alice@example.com", "bob@example.com"]
social_signups = ["charlie@example.com", "diana@example.com"]
# Merge and remove duplicates while preserving order
all_users = list(dict.fromkeys(email_signups + social_signups))
print(all_users)
# ['alice@example.com', 'bob@example.com', 'charlie@example.com', 'diana@example.com']
This approach keeps the first occurrence of each email, which is usually what you want when combining user lists.
When to Use Each Method
+operator: When you want a new list and readability matters most.extend()or+=: When you want to modify an existing list in place.*unpacking: When you need to merge multiple lists with extra elements in between, and you want clean syntax.itertools.chain: When you have many lists and memory efficiency is important.dict.fromkeys(): When you need to merge and deduplicate while preserving order.
A Quick Performance Note
If you’re merging lists inside a loop, avoid + at all costs. Here’s a simple benchmark you can try yourself:
import time
# Slow way
start = time.time()
result = []
for i in range(1000):
result = result + [i]
print("Using +:", time.time() - start)
# Fast way
start = time.time()
result = []
for i in range(1000):
result.append(i)
print("Using append:", time.time() - start)
The difference becomes dramatic with larger loops. Always prefer extend() or append() when building lists incrementally.
Merging with Conditions
Sometimes you don’t want to merge everything — you want to combine lists but skip certain elements. List comprehensions make this easy:
list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
merged = [x for x in list_a + list_b if x % 2 == 0]
print(merged) # [2, 4, 6, 8]
You can also merge with a condition on the source:
merged = [x for x in list_a if x > 2] + [x for x in list_b if x < 7]
The zip() Twist
Sometimes merging means pairing elements from two lists, not concatenating them. That’s what zip() does:
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
paired = list(zip(names, scores))
print(paired) # [('Alice', 85), ('Bob', 92), ('Charlie', 78)]
This is perfect for creating dictionaries or processing parallel data.
A Word on Nested Lists
If you have lists inside lists and you want to flatten them, don’t use + — that would just create a bigger list of lists. Use a list comprehension or itertools.chain as shown earlier. For deeply nested structures, you might need a recursive approach, but that’s a topic for another article.
The Takeaway
Merging lists in Python is one of those tasks that seems trivial but has several right answers depending on what you need. Here’s a quick cheat sheet:
- New list, simple merge: Use
+ - Modify existing list: Use
extend()or+= - Merge with extra elements: Use
*unpacking - Merge many lists efficiently: Use
itertools.chain - Merge and deduplicate preserving order: Use
dict.fromkeys() - Pair elements from two lists: Use
zip()
At PythonSkillset, we see developers overcomplicate this all the time. The right choice usually comes down to whether you need a new list or want to modify an existing one, and whether order or duplicates matter. Keep it simple, and your code will thank you.
Advertisement
Comments
Questions, corrections, and tips stay visible for everyone reading this page.
Join the discussion
No comments yet
Be the first to leave a note — it helps the next reader.