NumPy Tutorial: NumPy Concatenate and Stack

Jesse L
3 min readDec 28, 2021

Hi everyone, welcome back. NumPy is a library for the Python programming language. NumPy is short for “Numerical Python” and is a popular library that is used in data science. NumPy is used specifically to work with arrays as it provides various functions and support. In this tutorial, we will go over how we can combine multiple NumPy arrays together. With this introduction out of the way, let’s get into it.

Concatenate Example

Let’s start with the concatenate() function. The concatenate() function can be used to combine multiple arrays together. Let’s see an example:

import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
concatArray = np.concatenate((array1, array2))
print(concatArray)
Output:
[1 2 3 4 5 6]

In this example, we created two different arrays and used the concatenate() function to combine them. As we can see in the output, we have successfully created a new array with all the values in the two original arrays.

Stack Example

Now let’s look at the stack() function. The stack() function can also be used to combine multiple arrays together, but results in them stacking on top of each other. Let’s see an example:

import numpy as np
array1 =…

--

--

Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.