How to add one array to another array in Python

Key takeaways:

  • The NumPy library provides efficient method for adding arrays, making it ideal for handling numerical data.

  • Use numpy.add() for element-wise addition. This function adds corresponding elements of two arrays.

  • NumPy arrays can also be added using the + operator, which performs element-wise addition.

Adding one array to another is a common operation in Python, especially when working with numerical data. Python provides numpy.add() and + operator to perform element-wise addition.

Adding two arrays
Adding two arrays

Using NumPy and + operator to add arrays

In Python, it’s easy to add one array to another with the built-in Python library NumPy. It is a free Python library equipped with a collection of complex mathematical operations suitable for processing statistical data.

NumPy can be imported into Python as follows:

import numpy as np

1. Using the numpy.add() method

NumPy’s add() function allows element-wise addition of two arrays of the same shape. To add the two arrays together, we will pass both arrays as parameters to the add() method.

numpy.add(arr1,arr2)

To use this method, you must ensure that the two arrays are the same length.

import numpy as np
arr1 = np.array([3, 2, 1])
arr2 = np.array([1, 2, 3])
print ("1st array : ", arr1)
print ("2nd array : ", arr2)
out_arr = np.add(arr1, arr2)
print ("added array : ", out_arr)

2. Using the + operator

Python’s + operator can be used with NumPy arrays to perform element-wise addition, just like numpy.add().

import numpy as np
# Element-wise addition between NumPy arrays
arr1 = np.array([3, 2, 1])
arr2 = np.array([1, 2, 3])
out_arr = arr1 + arr2
print(out_arr)

Conclusion

In Python, adding one array to another is straightforward. The numpy.add() function and the + operator both perform element-wise addition efficiently. NumPy is ideal for handling numerical data, offering speed and convenience. Choosing between these methods depends on coding style and preference.

Become a Python developer with our comprehensive learning path!

Ready to kickstart your career as a Python Developer? Our “Become a Python Developer” path is designed to take you from your first line of code to landing your first job. This comprehensive journey offers essential knowledge, hands-on practice, interview preparation, and a mock interview, ensuring you gain practical, real-world coding skills. With our AI mentor by your side, you’ll overcome challenges with personalized support, building the confidence needed to excel in the tech industry.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do I add an array to another array in Python?

Use NumPy’s numpy.add() or the + operator for element-wise addition of arrays.


How do I combine two arrays into one array in Python?

Use the numpy.concatenate() method for combining arrays into one array.


How do you join an array to another array in Python?

We can use numpy.concatenate() or numpy.hstack() for NumPy arrays and extend() for lists.


How to append an array in Python?

Use numpy.append() method for NumPy arrays and append() for lists (to add a single element).


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved