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

enaJ
  • 1,565
  • 5
  • 16
  • 29

1 Answers1

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.

Community
  • 1
  • 1
farenorth
  • 10,165
  • 2
  • 39
  • 45