1

When creating an array using numpy, what is the difference between: 1) a = numpy.array((1,2,3)) and 2) a = numpy.array([1,2,3])?

1 Answer 1

1

There is no difference in the output.

a = np.array((1,2,3))
b = np.array([1,2,3])
(a == b).all() # True

The objects that those two commands create are identical.

You can also test equivalence with np.array_equal(a,b), see this question for more info.

Timing

Timing these two expressions has the tuple method with a marginal (insignificant?) advantage, for example in an iPython shell:

In [1]: %timeit a = np.array((1,2,3))
1000000 loops, best of 3: 1.04 µs per loop

In [2]: %timeit a = np.array([1,2,3])
1000000 loops, best of 3: 1.11 µs per loop

Running tests on longer (1 million entries) lists/tuples gives consistently marginal advantage to tuples.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Farenorth! That's very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.