## 1. Intro

In this tutorial, we will learn various ways to create NumPy array from the Python structure like the list, tuple and others. It will be helpful in use cases where we want to leverage the power of NumPy operations on existing data structures.

Python 3.6.5 and NumPy 1.15 is used. Visual Studio Code 1.30.2 used to run iPython interactive codes.

## 2. One dimensional NumPy array from Python list

We will use * numpy.array(object)* method to create 1-dimensional NumPy array from the Python list. List contains integer values.

#%% # Do some import import numpy as np print("Numpy Version is ", np.__version__) #%% # Creating 1 dimensional numpy array with Python list (int type) one_d_list = [1, 2, 3, 4, 5] array_one_dim_list = np.array(one_d_list) print("NumPy array: ", array_one_dim_list) print("Shape: ", array_one_dim_list.shape) print("Data Type: ", array_one_dim_list.dtype.name)

OUTPUT

Numpy Version is 1.15.4 NumPy array: [1 2 3 4 5] Shape: (5,) Data Type: int64

## 3. Two dimensional NumPy array from Python list

We will use * numpy.array(object)* method to create 2-dimensional NumPy array from the Python list. The list contains float values.

#%% # Creating 2 dimensional numpy array with Python list (float type) two_d_list = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] array_two_d_py_list = np.array(two_d_list) print("NumPy array: \n", array_two_d_py_list) print("Shape: ", array_two_d_py_list.shape) print("Data Type: ", array_two_d_py_list.dtype.name)

OUTPUT:

NumPy array: [[1. 2. 3.] [4. 5. 6.] [7. 8. 9.]] Shape: (3, 3) Data Type: float64

## 4. Three Dimensional NumPy array using Python list

We will use *numpy.array(object)* method to create 3-dimensional NumPy array from the Python list. The list contains String values.

Non-number values in NumPy array defies the purpose of it. However, it is possible to create String data type NumPy array.

#%% # Creating 3 dimensional numpy array with Python 3d list (String) three_d_list = [ [["aa", "bb", "cc"], ["dd", "ee", "ff"], ["gg", "hh", "kk"]], [["ll", "mm", "nn"], ["oo", "pp", "qq"], ["rr", "ss", "tt"]], ] three_d_array = np.array(three_d_list) print("NumPy array: \n", three_d_array) print("Shape: ", three_d_array.shape) print("Data Type: ", three_d_array.dtype.name)

OUTPUT:

NumPy array: [[['aa' 'bb' 'cc'] ['dd' 'ee' 'ff'] ['gg' 'hh' 'kk']] [['ll' 'mm' 'nn'] ['oo' 'pp' 'qq'] ['rr' 'ss' 'tt']]] Shape: (2, 3, 3) Data Type: str64

## 5. NumPy array using Python list with mix data type elements

We will use * numpy.array(object)* method to create a 1-dimensional NumPy array from the Python list. The list contains integer and float values.

#%% # Creating NumPy array with Python list with mix data type elements mix_data_type_list = [1.0, 2, 3.5, 4, 5.0] mix_data_type_array = np.array(mix_data_type_list) print("NumPy array: \n", mix_data_type_array) print("Shape: ", mix_data_type_array.shape) print("Data Type: ", mix_data_type_array.dtype.name)

OUTPUT:

NumPy array: [1. 2. 3.5 4. 5. ] Shape: (5,) Data Type: float64

NumPy support homogeneous elements in array. While creating when NumPy found heterogeneous elements (float and int) Python list, it automatically convert the int elements to float.

NumPy cast an element to larger byte data type element while creating an array, It is called *upcasting. *

## 6. Specify data type while creating NumPy array

We will use the same Python array used in the previous code block to create new NumPy array. However, we will force a data type of NumPy array.

#%% # Creating NumPy array with Pythong list and specifying data type fix_data_type_array = np.array(mix_data_type_list, np.int) print("NumPy array: \n", fix_data_type_array) print("Shape: ", fix_data_type_array.shape) print("Data Type: ", fix_data_type_array.dtype.name)

OUTPUT:

NumPy array: [1 2 3 4 5] Shape: (5,) Data Type: int64

We forced NumPy to make array elements of int data type by passing an argument to *np.array(object, datatype) *and it forced conversion of float into an int.

In absence of this parameter, NumPy should upcast int elements as a float.

## 7. Create a NumPy array with Python list and tuple

#%% # Creating NumPy array with mix of List and Tuples upgraded a_list = [1, 2.5, 3] a_tuple = (1.5 , 2.3, 3) two_d_list_tuple_array = np.array([a_list, a_tuple]) print("NumPy array: \n", two_d_list_tuple_array) print("Shape: ", two_d_list_tuple_array.shape) print("Data Type: ", two_d_list_tuple_array.dtype.name)

OUTPUT:

NumPy array: [[1. 2.5 3. ] [1.5 2.3 3. ]] Shape: (2, 3) Data Type: float64

## 8. NumPy array with Jagged Python List

We will create NumPy array with jagged 2 Dimensional Python list and observe the outcome data type.

#%% # Creating NumPy array with jagged 2 d Python list jagged_two_d_list = [[1, 2, 3], [4, 5, 6], [7, 8]] jagged_two_d_array = np.array(jagged_two_d_list) print("NumPy array: \n", jagged_two_d_array) print("Shape: ", jagged_two_d_array.shape) print("Data Type: ", jagged_two_d_array.dtype.name)

NumPy array: [list([1, 2, 3]) list([4, 5, 6]) list([7, 8])] Shape: (3,) Data Type: object

When we pass jagged array to *numpy.array()* method, NumPy create array with Object elements.

## 9. NumPy array with minimum dimension

We can enforce minimum dimension for NumPy array created using *numpy.array(object = list, ndim = 3)*, even passed Python list is not of specified dimension.

#%% # Create NumPy array with Python List and enforce minimum dimension # We are using an already create one dimensional array one_d_list array_enforced_three_dim_list = np.array(object=one_d_list, ndmin=3) print("NumPy array: ", array_enforced_three_dim_list) print("Shape: ", array_enforced_three_dim_list.shape) print("Data Type: ", array_enforced_three_dim_list.dtype.name)

OUTPUT:

NumPy array: [[[1 2 3 4 5]]] Shape: (1, 1, 5) Data Type: int64

NumPy pre-pend Ones to the shape as needed to meet the minimum dimensional requirement.

## 10. *asarray* method

*asarray*

NumPy * asarray* method doesn’t copy an object if not required, while

*method copy object as their default option. While we can change the default behaviour by passing false to copy argument*array

*array(object = list, copy = false)*

#%% # Use asarray to create NumPy array one_dim_list = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] one_dim_array_use_asarray = np.asarray(one_dim_list) print("NumPy array: \n", one_dim_array_use_asarray) print("Shape: ", one_dim_array_use_asarray.shape) print("Data Type: ", one_dim_array_use_asarray.dtype.name)

OUTPUT:

NumPy array: [[ 1 2 3 4 5] [ 6 7 8 9 10]] Shape: (2, 5) Data Type: int64

When Python list used, NumPy *asarray* had copied object, because they need to convert it into NumPy array.

But when we use one NumPy array to create new array, *asarray* doesn’t copy.

#%% # array method by default make copy and hence change applied to copy np.array(one_dim_array_use_asarray)[1]=1 print("NumPy array: \n", one_dim_array_use_asarray) # asarray method don't make copy and hence change applied to one_dim_array_use_asarray np.asarray(one_dim_array_use_asarray)[1]=1 print("NumPy array: \n", one_dim_array_use_asarray)

OUTPUT:

NumPy array: [[ 1 2 3 4 5] [ 6 7 8 9 10]] NumPy array: [[1 2 3 4 5] [1 1 1 1 1]]

We saw how * asarray* don’t copy if not needed in contrast with

*array*method behaviour.

## 11. Conclusion

We learned various ways to create

Download source code related to this tutorial here

- Python – create-array-with-python-structures.py
- Jupyter – create-array-with-python-structures.ipynb

Run the Jupyter notebook for this tutorial.

Cook AllenNice post! Thanks for sharing this.