enaJ
enaJ

Reputation: 1655

Python Array Creation: Difference between a = numpy.array((1,2,3)) and a = numpy.array([1,2,3])

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])?

Upvotes: 1

Views: 133

Answers (1)

farenorth
farenorth

Reputation: 10781

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.

Upvotes: 1

Related Questions