Last modified: Aug 14, 2026
Python Array Concatenation Guide
Combining arrays is a common task in Python. This guide explains the main ways to concatenate arrays. You will learn simple methods and see clear examples. We'll cover both basic lists and NumPy arrays. This will help you choose the right approach for your code.
Why Concatenate Arrays?
You often need to merge data from different sources. For example, you might have two lists of user names. Or you might need to combine results from different calculations. Concatenation makes this easy. It creates a new array with all elements from the original ones.
The method you choose depends on your needs. Some methods change the original array. Others create a new one. We'll explain these differences clearly.
Using the + Operator for Lists
The simplest way to concatenate two lists is with the + operator. It creates a new list. The original lists remain unchanged. This is clean and readable.
# Basic list concatenation
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print(combined)
# Output: [1, 2, 3, 4, 5, 6]
You can also concatenate more than two lists. Just chain the + operator. The result is a single flat list. This is great for merging multiple datasets.
# Concatenating three lists
a = [1, 2]
b = [3, 4]
c = [5, 6]
result = a + b + c
print(result)
# Output: [1, 2, 3, 4, 5, 6]
This method is straightforward. It's perfect for beginners. If you need to keep the original lists for later use, use this approach. It's non-destructive.
Using extend() to Modify in Place
The extend() method adds elements from another list to the end. It modifies the original list. It does not return a new list. This is useful when you want to update an existing list.
# Using extend()
original = [1, 2, 3]
extra = [4, 5]
original.extend(extra)
print(original)
# Output: [1, 2, 3, 4, 5]
Notice that extend() returns None. So you can't assign its result to a variable. It changes the list in place. This is more memory-efficient for large lists. It avoids creating a new list object.
Be careful with this method. It changes your original data. If you need the original list later, make a copy first. You can use list.copy() or slicing. For more list operations, check our Python Array Functions Guide.
Using * for Repetition
The * operator repeats a list. It's not exactly concatenation. But it's useful for creating patterns. It multiplies the list content.
# Repeating a list
base = [0, 1]
repeated = base * 3
print(repeated)
# Output: [0, 1, 0, 1, 0, 1]
This is great for initializing lists. For example, you can create a list of zeros. Or you can repeat a pattern. It creates a new list. The original list is not changed.
Concatenating NumPy Arrays
If you work with numerical data, you likely use NumPy. NumPy arrays are different from Python lists. They are more efficient for large datasets. You concatenate them with numpy.concatenate().
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
combined = np.concatenate((arr1, arr2))
print(combined)
# Output: [1 2 3 4 5 6]
Notice we pass a tuple of arrays to concatenate(). This function works along an existing axis. By default, it flattens the arrays. For 1D arrays, this is simple concatenation.
For 2D arrays, you can specify the axis. Use axis=0 for rows and axis=1 for columns. This is powerful for matrix operations. If you're working with multi-dimensional data, this is essential.
import numpy as np
# 2D arrays
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
# Concatenate rows (axis=0)
row_wise = np.concatenate((arr1, arr2), axis=0)
print(row_wise)
# Output:
# [[1 2]
# [3 4]
# [5 6]]
Make sure the shapes are compatible. For axis=0, the number of columns must match. For axis=1, the number of rows must match. Otherwise, you'll get an error. If you need to understand array shapes better, see our Python Array vs NumPy Array guide.
Using append() for Simple Cases
The append() method adds a single element to the end of a list. It's not for merging two lists. But it's useful in loops. You can build a new list step by step.
# Building a list with append()
new_list = []
for i in range(5):
new_list.append(i * 2)
print(new_list)
# Output: [0, 2, 4, 6, 8]
This is a common pattern. It's clear and easy to read. But it can be slow for large loops. In that case, list comprehensions are faster. We'll cover those next.
List Comprehensions for Concatenation
List comprehensions are a Pythonic way to create lists. You can use them to concatenate lists conditionally. This is more powerful than simple concatenation.
# Using list comprehension to filter and combine
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
# Combine only even numbers
combined = [x for x in list1 + list2 if x % 2 == 0]
print(combined)
# Output: [2, 4, 6, 8]
This is a concise way to merge and filter in one step. It's clean and efficient. It creates a new list. The original lists are unchanged. This is great for data cleaning tasks.
Performance Considerations
When working with large arrays, performance matters. The + operator creates a new list. This is fine for small lists. But for large lists, it uses more memory. The extend() method is more efficient. It modifies the list in place.
For NumPy arrays, numpy.concatenate() is the best choice. It's optimized for numerical data. It's much faster than converting to lists and back. Always use the right tool for the job.
If you need to insert elements at a specific position, check our guide on Python Array Insert. It shows how to add elements anywhere, not just at the end.
Common Mistakes to Avoid
One common mistake is mixing lists and NumPy arrays. The + operator on NumPy arrays does element-wise addition, not concatenation. This is a big difference. Always use numpy.concatenate() for NumPy arrays.
import numpy as np
arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
# This does element-wise addition, not concatenation!
wrong = arr1 + arr2
print(wrong)
# Output: [4 6]
Another mistake is forgetting that extend() returns None. If you try to assign its result, you'll get None. This can cause bugs. Always use it as a statement, not an expression.
Also, be careful with nested lists. The + operator doesn't flatten nested lists. It just concatenates the outer lists. If you need to flatten, use itertools.chain() or a list comprehension.
Conclusion
Concatenating arrays in Python is simple. You have several options. The + operator is best for lists. It's clean and non-destructive. The extend() method is efficient for in-place updates. For NumPy arrays, use numpy.concatenate(). It's fast and powerful.
Each method has its use case. Choose based on your needs. If you're a beginner, start with the + operator. It's the easiest to understand. As you get more comfortable, explore the other methods.
Remember to test your code. Use print statements to verify the results. This will help you catch errors early. With these tools, you can handle any array merging task. For more array operations, explore our Python Array Indexing Guide to master array manipulation.