How to Concatenate Arrays in Python?

Estimated read time 1 min read

To concatenate arrays in Python, you can use the numpy library. NumPy provides a function called concatenate() that allows you to concatenate arrays along a specified axis. Here’s an example of how you can do it:

import numpy as np

# Create two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Concatenate the arrays
concatenated_array = np.concatenate((array1, array2))

# Print the concatenated array

In the code above, we first import the numpy library as np. Then, we create two arrays, array1 and array2, using the np.array() function.

To concatenate the arrays, we use the np.concatenate() function and pass it the arrays we want to concatenate as a tuple (array1, array2). The np.concatenate() function returns the concatenated array.

Finally, we print the concatenated array using the print() function.

The result will be [1 2 3 4 5 6], which is the concatenation of array1 and array2 into a single array.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply