-3

I have a quick question.

I would like to know if : array() is the same as []

An example:

Is :

[[1,2,2],[1,2,8]]

Equal to :

array([1,2,2],[1,2,8])

? Could you explain the differences and examples of uses pls.

My code :

import numpy as np
x = np.array(([1,2,2], [1,4,3], [1,2,9]))
x = np.full(x.shape, 10)
print(x)

I'm getting :

[[10,10,10],[10,10,10],[10,10,10]]

and I would like :

array([10,10,10], [10,10,10], [10,10,10])
LuckyFr
  • 401
  • 1
  • 5
  • 19
  • 2
    Does this answer your question? [Python: Differences between lists and numpy array of objects](https://stackoverflow.com/questions/15944171/python-differences-between-lists-and-numpy-array-of-objects) – Reznik Mar 12 '21 at 18:17
  • https://stackoverflow.com/questions/176011/python-list-vs-array-when-to-use – Chris_Rands Mar 12 '21 at 18:18
  • thanks yeah I saw all those pages thanks. The fact is I have [[]] and i want array([]) how to do that? – LuckyFr Mar 12 '21 at 18:19
  • The second example throws an exception. – Thomas Weller Mar 12 '21 at 18:22
  • For one thing, the first version with hard brackets is a list of lists, and the second version with parentheses is either a syntax error or a `TypeError`. If you want to use numpy arrays, you'll have to `import numpy` and read through the [Relevant documentation](https://numpy.org/doc/stable/reference/generated/numpy.array.html) – G. Anderson Mar 12 '21 at 18:23
  • Thank you Anderson, I know all this already. – LuckyFr Mar 12 '21 at 18:25
  • 1
    The reason the initial comments didn't give any useful information is because the initial question wasn't asked in a useful way. Since you edited to include a [mcve], the question makes more sense. The reason you're seeing it without `array()` around is is the `print()` call. When you print an array, the `repr` is to just display the contents as a list of lists – G. Anderson Mar 12 '21 at 18:36
  • So at the end it's only the print formatting the output ? @G.Anderson – LuckyFr Mar 12 '21 at 18:38
  • Exactly. Try for yourself with `np.array(([1,2,2], [1,4,3], [1,2,9])); print(x)` and `np.array([[1,2,2], [1,4,3], [1,2,9]]); print(x)`: Spoiler alert: It makes no difference to numpy how the array is built, a tuple of lists (`()`) or a list of lists (`[]`) are both iterable – G. Anderson Mar 12 '21 at 18:43
  • Ah ok so it means that : array() = [[]] ? @G.Anderson – LuckyFr Mar 12 '21 at 18:48
  • No. Per the linked duplicate, `x=[[1,2],[3,4]]` is a _list object_ that contains _list objects_, a list of lists. `x=([1,2],[3,4])` is a _tuple object_ containing _list objects_, a tuple of lists. Either can then be used in a call to the _numpy_ `.array(x)` constructor, which transforms the list of lists or tuple of lists into a single _numpy array object_ – G. Anderson Mar 12 '21 at 18:52
  • 1
    @LuckyFr **no** `array()` does not `==` `[[]]`. You are merely seeing how numpy.ndarray objects are *printed*. Don't worry too much about what you see when you `print` something. Check the `type` of the object. You'll see your code produces a `numpy.ndarray` object, not a `list`, if that is what you are afraid of – juanpa.arrivillaga Mar 12 '21 at 18:54
  • Sp @G.Anderson, array([[1,2,3],[1,2,4]]) is a tuple object ? – LuckyFr Mar 12 '21 at 18:55
  • 1
    @LuckyFr no. You can *figure this out for yourself by running the code and checking the type* – juanpa.arrivillaga Mar 12 '21 at 18:55
  • Wait, I tried G.Anderson example. my print is giving exactly the same thing – LuckyFr Mar 12 '21 at 18:59
  • I'm reopening this because the proposed duplicate answers the subject line, but not the example, https://stackoverflow.com/questions/15944171/python-differences-between-lists-and-numpy-array-of-objects – hpaulj Mar 12 '21 at 19:35
  • Thank you @hpaulj – LuckyFr Mar 12 '21 at 19:36

1 Answers1

1

A lot of the comments focused on the subject line, the difference between list and numpy array. But the example is all about the display of a numpy array.

Your example:

In [272]: x = np.array(([1,2,2], [1,4,3], [1,2,9]))
     ...: x = np.full(x.shape, 10)
In [273]: x
Out[273]: 
array([[10, 10, 10],
       [10, 10, 10],
       [10, 10, 10]])
In [274]: print(x)
[[10 10 10]
 [10 10 10]
 [10 10 10]]

That print is the str display of an array. Note the missing commas. The repr display includes the commas and word 'array'

In [275]: print(repr(x))
array([[10, 10, 10],
       [10, 10, 10],
       [10, 10, 10]])

The display of a list:

In [276]: x.tolist()
Out[276]: [[10, 10, 10], [10, 10, 10], [10, 10, 10]]

The 'duplicate' that focuses on list versus array:

Python: Differences between lists and numpy array of objects

Some examples from the comments:

In [277]: np.array()
Traceback (most recent call last):
  File "<ipython-input-277-e4f3b47dc252>", line 1, in <module>
    np.array()
TypeError: array() missing required argument 'object' (pos 1)

Making a 2d array without any elements:

In [278]: np.array([[]])
Out[278]: array([], shape=(1, 0), dtype=float64)

Making a 0d array with 1 element:

In [279]: np.array(34)
Out[279]: array(34)
In [280]: np.array(34).shape
Out[280]: ()

Arrays can a wide range of dimensions and shape, from 0d up (max 32). Depending on where you come from dimensions other than 2 can be hard to picture.

The display of arrays and lists generally match in the nesting of brackets.

hpaulj
  • 221,503
  • 14
  • 230
  • 353