A python list can contain disparately typed objects, e.g. X = ['apples', 'oranges',10]. If you do type([10]) you'll see the Python type for the container is technically called a list, not an array.

In contrast, in a numpy array all objects are of the same type, the dtype.

The docs are telling you that on creation of a numpy array, the dtype is set to the type that will hold all of the existing objects.

See, look:

the type will be determined as the minimum type required to hold the objects in the sequence

The writers perhaps should have added "and not their values"

We can make a uint8 ten easily enough:

ten = np.uint8(10)

If that is put into a Python list, it retains its type because Python lists preserve types. If that list is sent to numpy.array() to make a numpy array, then the numpy array will use dtype np.uint8 because it is big enough to hold all (1) of the pre-existing Python list objects.

In [49]: np.array([ten]).dtype
Out[49]: dtype('uint8')

But if we use a literal 10, python will create an int object for it instead of an np.uint8 because np.uint8 is peculiar to numpy and all 10 does is invoke python to create that number.

If we make a Python list containing a literal 10, we duplicate your result (with machine-architecture ints):

In [50]: np.array([10]).dtype
Out[50]: dtype('int64')

And if we put the two types together in a python list, and send that list to np.array for creation of a numpy array, then the dtype must be big enough to hold both objects, in this case int64.

In [51]: np.array([ten, 10]).dtype
Out[51]: dtype('int64')
Answer from Paul on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ arrays.dtypes.html
Data type objects (dtype) โ€” NumPy v2.5 Manual
This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description. ... Try it in your browser! Data-type with fields big (big-endian 32-bit integer) and little (little-endian 32-bit integer): ... This style has two required and three optional keys. The names and formats keys are required. Their respective values are equal-length lists ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_data_types.asp
NumPy Data Types
Below is a list of all data types in NumPy and the characters used to represent them. ... import numpy as np arr = np.array(['apple', 'banana', 'cherry']) print(arr.dtype) Try it Yourself ยป
Top answer
1 of 1
4

A python list can contain disparately typed objects, e.g. X = ['apples', 'oranges',10]. If you do type([10]) you'll see the Python type for the container is technically called a list, not an array.

In contrast, in a numpy array all objects are of the same type, the dtype.

The docs are telling you that on creation of a numpy array, the dtype is set to the type that will hold all of the existing objects.

See, look:

the type will be determined as the minimum type required to hold the objects in the sequence

The writers perhaps should have added "and not their values"

We can make a uint8 ten easily enough:

ten = np.uint8(10)

If that is put into a Python list, it retains its type because Python lists preserve types. If that list is sent to numpy.array() to make a numpy array, then the numpy array will use dtype np.uint8 because it is big enough to hold all (1) of the pre-existing Python list objects.

In [49]: np.array([ten]).dtype
Out[49]: dtype('uint8')

But if we use a literal 10, python will create an int object for it instead of an np.uint8 because np.uint8 is peculiar to numpy and all 10 does is invoke python to create that number.

If we make a Python list containing a literal 10, we duplicate your result (with machine-architecture ints):

In [50]: np.array([10]).dtype
Out[50]: dtype('int64')

And if we put the two types together in a python list, and send that list to np.array for creation of a numpy array, then the dtype must be big enough to hold both objects, in this case int64.

In [51]: np.array([ten, 10]).dtype
Out[51]: dtype('int64')
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.types.html
Data types โ€” NumPy v2.5 Manual
NumPy generally returns elements of arrays as array scalars (a scalar with an associated dtype). Array scalars differ from Python scalars, but for the most part they can be used interchangeably (the primary exception is for versions of Python older than v2.x, where integer array scalars cannot act as indices for lists and tuples).
๐ŸŒ
Python Course
python-course.eu โ€บ numerical-programming โ€บ numpy-data-objects-dtype.php
3. Numpy Data Objects, dtype | Numerical Programming
The elements of a list named lst are then converted to the i16 type to create a two-dimensional array called A. import numpy as np i16 = np.dtype(np.int16) print(i16) lst = [ [3.4, 8.7, 9.9], [1.1, -7.8, -0.7], [4.1, 12.3, 4.8] ] A = np.array(lst, dtype=i16) print(A)
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ arrays.dtypes.html
Data type objects (dtype) โ€” NumPy v2.1 Manual
This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description. ... This style has two required and three optional keys. The names and formats keys are required. Their respective values are equal-length lists with the field names and the field formats.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: astype() to change dtype of an array | note.nkmk.me
February 4, 2024 - When the dtype is set to object, you can freely increase the number of characters in a string. a_object[2] = 'abcXYZ' print(a_object) # [1 0.1 'abcXYZ'] ... Arrays containing elements of different types can also be represented using Python's built-in list type. list and ndarray behave differently with operators. While ndarray supports element-wise operations, creating and processing such data in NumPy might be less common, considering the versatility of list for handling mixed types.
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ arrays.dtypes.html
Data type objects (dtype) โ€” NumPy v2.3 Manual
This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description. ... Try it in your browser! Data-type with fields big (big-endian 32-bit integer) and little (little-endian 32-bit integer): ... This style has two required and three optional keys. The names and formats keys are required. Their respective values are equal-length lists with the field names and the field formats.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.ndarray.dtype.html
numpy.ndarray.dtype โ€” NumPy v2.2 Manual
Data-type of the arrayโ€™s elements. ... Setting arr.dtype is discouraged and may be deprecated in the future.
๐ŸŒ
APXML
apxml.com โ€บ courses โ€บ essential-numpy-pandas โ€บ chapter-2-getting-started-numpy-arrays โ€บ array-data-types
NumPy Array Data Types (dtypes)
This is different from standard ... optimized, low-level implementations of numerical operations. The specific data type of an array's elements is stored in a special attribute called dtype (short for data type)....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ arrays.dtypes.html
Data type objects (dtype) โ€” NumPy v2.2 Manual
This style does not accept align in the dtype constructor as it is assumed that all of the memory is accounted for by the array interface description. ... This style has two required and three optional keys. The names and formats keys are required. Their respective values are equal-length lists with the field names and the field formats.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69305430 โ€บ numpy-array-custom-dtype-for-the-list-of-list
python - numpy array custom dtype for the list of list - Stack Overflow
I am confusing in using custom dtype of numpy array which is converting the element of the list to be tuple. np.empty (1000, dtype = [('a',int), ('b','S4')]) this would result an array with a tupl...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.dtype.html
numpy.dtype โ€” NumPy v2.5 Manual
The array-protocol typestring of this data-type object. ... Tuple (item_dtype, shape) if this dtype describes a sub-array, and None otherwise.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_data_types.htm
NumPy - Data Types
Original array: [1 2 3 4 5] Original dtype: int64 Converted array: [1. 2. 3. 4. 5.] Converted dtype: float32 ยท NumPy also provides functions for casting arrays to specific types.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.ndarray.dtype.html
numpy.ndarray.dtype โ€” NumPy v2.1 Manual
Setting will replace the dtype without modifying the memory (see also ndarray.view and ndarray.astype). ... Cast the values contained in the array to a new data-type.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.ndarray.dtype.html
numpy.ndarray.dtype โ€” NumPy v2.5 Manual
January 31, 2021 - >>> import numpy as np >>> x = np.arange(4).reshape((2, 2)) >>> x array([[0, 1], [2, 3]]) >>> x.dtype dtype('int64') # may vary (OS, bitness) >>> isinstance(x.dtype, np.dtype) True