What happens when we apply an operation between two 1-D arrays with different lengths?

Question

In NumPy, what happens when we apply an operation between two 1-D arrays with different lengths?

Answer

If we try to perform an operation, such as addition or subtraction, on two or more arrays with differing lengths, then there will be an error message thrown, specifically a ValueError.

When performing an operation on two arrays, each element of both arrays is essentially paired up from the first to the last element, and the operation is run on each pair of values. When one element is unpaired due to different array lengths, it will have no paired value, causing the error.

5 Likes

What is the easiest way to perform operations with arrays with differing lengths?