By Ayush Goyal


2018-03-13 15:45:10 8 Comments

I am learning python, and I recently came across a module Numpy. With the help of Numpy, one can convert list to arrays and perform operations much faster. Let's say we create an array with following values :

import numpy as np
np_array=array([1,2,3,4,5])

So we need one square bracket if we need to store one list in the form of array. Now if I want to create a 2D array, why it should be defined like this:

np_array=array([[1,2,3,4,5],[6,7,8,9,10]])

And not like this:

np_array=array([1,2,3,4,5],[6,7,8,9,10])

I apologize if this question is a duplicate, but I couldn't find any answer.

Many Thanks

2 comments

@DeepSpace 2018-03-13 15:48:43

This actually has little to do with numpy. You are essentially asking what is the difference between foo(a, b) and foo([a, b]).

arbitrary_function([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]) passes two lists as separate arguments to arbitrary_function (one argument is [1, 2, 3, 4, 5] and the second is [6, 7, 8, 9, 10]).

arbitrary_function([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) passes a list of lists ([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) to arbitrary_function.

Now, numpy creators could have chosen to allow
arbitrary_function([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]) but it would have made little to no sense to do so.

@Ayush Goyal 2018-03-13 15:56:26

Thank you for such a detailed answer. This cleared all the doubts :)

@Axeon Thra 2018-03-13 15:48:53

Array function has the following form.

array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)

If you use

np_array=array([1,2,3,4,5],[6,7,8,9,10])

The function call will result in passing [1,2,3,4,5] to object and [6,7,8,9,10] to dtype, which wont make any sense.

@Ayush Goyal 2018-03-13 15:50:52

That was helpful! Thank you :)

@DeepSpace 2018-03-13 15:56:04

One could then ask why array's signature is not array(*objects, dtype=None, copy=True, order=None, subok=False, ndmin=0) and the answer would be "because it wouldn't make any sense" and that it would probably go against several conventions (and would also probably complicate array's implementation)

Related Questions

Sponsored Content

5 Answered Questions

[SOLVED] Use numpy array in shared memory for multiprocessing

5 Answered Questions

[SOLVED] Converting a 3D List to a 3D NumPy array

4 Answered Questions

[SOLVED] Shift elements in a numpy array

  • 2015-05-22 14:36:42
  • chrisaycock
  • 32991 View
  • 38 Score
  • 4 Answer
  • Tags:   python numpy

6 Answered Questions

[SOLVED] Why NumPy instead of Python lists?

  • 2009-06-14 23:02:14
  • Thomas Browne
  • 115533 View
  • 348 Score
  • 6 Answer
  • Tags:   python numpy

2 Answered Questions

[SOLVED] Using numpy arrays of sympy numbers

4 Answered Questions

2 Answered Questions

[SOLVED] numpy array specify a default

  • 2015-06-23 17:48:14
  • py_works
  • 454 View
  • 2 Score
  • 2 Answer
  • Tags:   python numpy

3 Answered Questions

[SOLVED] Removing duplicates from a list of numPy arrays

4 Answered Questions

2 Answered Questions

[SOLVED] Least square difference among items of a numpy array

  • 2012-12-06 18:16:33
  • biojl
  • 806 View
  • 4 Score
  • 2 Answer
  • Tags:   python numpy

Sponsored Content