How to Concatenate Arrays in Python

While working on a data project, I needed to merge multiple arrays into a single one. At first, I thought it would be simple, but I quickly realized there are many different ways to do it in Python, depending on the type of array I was working with.

Over the years, as a Python developer, I’ve used array concatenation in data analysis, machine learning, and even in automation scripts. It’s one of those operations you’ll use often, so it’s worth knowing the different methods and when to use each.

In this tutorial, I’ll show you seven simple ways to concatenate arrays in Python. I’ll start with the built-in approaches and then move on to NumPy.

Concatenation in Python

Concatenation simply means joining two or more arrays into a single array.

For example, if I have:

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]

Concatenating them gives me:

[1, 2, 3, 4, 5, 6]

That’s the basic idea. Now, let’s look at the different methods to achieve this in Python.

Method 1 – Use the + Operator

The simplest way to concatenate Python arrays (lists in Python) is by using the + operator.

Here’s an example:

arr1 = [10, 20, 30]
arr2 = [40, 50, 60]

result = arr1 + arr2
print(result)

Output:

[10, 20, 30, 40, 50, 60]

I executed the above example code and added the screenshot below.

concatenate arrays python without numpy

I often use this when I just need a quick merge of two lists. It’s clean and easy to read.

Method 2 – Use the extend() Method

Another way is to use the extend() method, which modifies the original list.

arr1 = [100, 200, 300]
arr2 = [400, 500, 600]

arr1.extend(arr2)
print(arr1)

Output:

[100, 200, 300, 400, 500, 600]

I executed the above example code and added the screenshot below.

python concatenate arrays

Notice that this changes arr1 directly. I use this when I don’t need to keep the original array unchanged.

Method 3 – Use a Loop

Sometimes, especially when working with custom objects, I prefer to use a loop.

arr1 = ["New York", "Chicago"]
arr2 = ["Los Angeles", "Houston"]

for city in arr2:
    arr1.append(city)

print(arr1)

Output:

['New York', 'Chicago', 'Los Angeles', 'Houston']

I executed the above example code and added the screenshot below.

concatenate arrays

This method is slower for large datasets, but it gives me more control if I need to add conditions while merging.

Method 4 – Use NumPy concatenate()

When working with numerical data, NumPy is the go-to library. It has a built-in concatenate() function.

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

result = np.concatenate((arr1, arr2))
print(result)

Output:

[1 2 3 4 5 6]

I executed the above example code and added the screenshot below.

concatenate arrays python

This is my preferred method when I’m working with scientific or machine learning data.

Method 5 – Use NumPy hstack() and vstack()

NumPy also provides hstack() (horizontal stack) and vstack() (vertical stack).

Horizontal Stack Example

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

result = np.hstack((arr1, arr2))
print(result)

Output:

[1 2 3 4 5 6]

Vertical Stack Example

arr1 = np.array([[1, 2, 3]])
arr2 = np.array([[4, 5, 6]])

result = np.vstack((arr1, arr2))
print(result)

Output:

[[1 2 3]
 [4 5 6]]

I use hstack when I want to merge arrays side by side, and vstack when I want to stack them on top of each other.

Method 6 – Use NumPy stack()

Python’s stack() function lets you add a new dimension while concatenating.

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

result = np.stack((arr1, arr2), axis=0)
print(result)

Output:

[[1 2 3]
 [4 5 6]]

This is useful when I want to create a 2D array from two 1D arrays.

Method 7 – Use itertools.chain()

Finally, Python’s itertools module has a chain() function that works like concatenation.

from itertools import chain

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
arr3 = [7, 8, 9]

result = list(chain(arr1, arr2, arr3))
print(result)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

I use this when I want to concatenate multiple arrays without creating intermediate lists.

Which Method Should You Use?

  • If you’re working with small lists, use + or extend().
  • If you need performance with numbers, go with NumPy (concatenate, hstack, vstack).
  • If you’re combining many lists, itertools.chain() is memory-efficient.

Practical Example: Combine Sales Data from Multiple States

Let’s say I’m analyzing sales data from different states in the USA. Each state’s data is in an array, and I want to combine them into one.

import numpy as np

california_sales = np.array([1200, 1500, 1600])
texas_sales = np.array([1100, 1300, 1250])
florida_sales = np.array([1000, 1150, 1400])

all_sales = np.concatenate((california_sales, texas_sales, florida_sales))
print("Combined Sales Data:", all_sales)

Output:

Combined Sales Data: [1200 1500 1600 1100 1300 1250 1000 1150 1400]

This is a real-world scenario where array concatenation makes reporting and analysis much easier.

Most of the time, I find myself using NumPy’s concatenate() or hstack() because they are fast and reliable. But it’s good to know all the different methods, because sometimes the simplest solution (+ operator) is all you need.

Once you start working with larger datasets, especially in data science or machine learning projects, you’ll appreciate the efficiency of NumPy.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.