How to Merge Two Lists in Python (Concatenate & Extend)
Learn the best ways to merge and combine two lists in Python. Compare the plus operator, extend method, list unpacking, and chain options.
Explanation
Combining list datasets is a frequent operation when aggregating results, processing batches of data, or appending items to a collection. Python provides several ways to merge lists, depending on whether you want to modify a list in-place or create a new list.
To create a brand-new list by combining two existing ones, the most straightforward approach is using the concatenation operator `+` (e.g., `list3 = list1 + list2`). Alternatively, in Python 3.5+, you can use list unpacking syntax `[*list1, *list2]`, which is highly expressive and allows you to easily inject additional elements anywhere in the list creation.
If you want to modify an existing list in-place by adding elements from another list, the `.extend()` method is the most efficient choice. This avoids allocating memory for a new list and modifies the target list directly. For memory-efficient traversal of merged lists without duplicating data in memory, you can use `itertools.chain()`.
Step-by-Step Implementation
- 1
Use the + operator to merge lists and return a new combined list.
- 2
Call list_a.extend(list_b) to append all elements of list_b directly into list_a.
- 3
Use the asterisk unpacking operator [*list_a, *list_b] to construct a list from elements of both.
Code Example
This script demonstrates merging two lists using operators, extend, and unpacking techniques.
list_a = [1, 2, 3]
list_b = [4, 5, 6]
# 1. Plus operator (creates a new list)
merged_plus = list_a + list_b
print("Using + operator:", merged_plus)
# 2. Unpacking operator (creates a new list)
merged_unpack = [*list_a, *list_b]
print("Using unpacking:", merged_unpack)
# 3. Extend method (modifies list_a in-place)
list_a.extend(list_b)
print("After extending list_a:", list_a)Using + operator: [1, 2, 3, 4, 5, 6]
Using unpacking: [1, 2, 3, 4, 5, 6]
After extending list_a: [1, 2, 3, 4, 5, 6]Frequently Asked Questions
What is the difference between append() and extend()?
append() adds the entire list argument as a single nested element, whereas extend() unpacks the list argument and adds each element individually.
How do I merge lists while removing duplicates?
You can merge them and then cast the result to a set, or convert them to sets first: list(set(list_a) | set(list_b)).
Related How-To Guides
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Lists
Learn everything about Python lists. Discover how to create, slice, modify, and iterate through arrays in Python natively.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.