Skip to main content

Create NumPy array from Python list and tuples

1. Intro

In this tutorial, we will learn various ways to create a 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 are 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 a 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 a 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 a 3-dimensional NumPy array from the Python list. The list contains String values.

Non-number values in the NumPy array define its purpose of it. However, it is possible to create a 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 mixed 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 supports homogeneous elements in the array. While creating when NumPy found heterogeneous elements (float and int) Python list, it automatically converts the int elements to float.

NumPy cast an element to a 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 a 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 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 a 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)

OUTPUT:

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

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

9. NumPy array with a minimum dimension

We can enforce minimum dimension for the 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

NumPy asarray method doesn't copy an object if not required, while array method copy object as their default option. While we can change the default behavior by passing false to copy argument 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 the Python list was used, NumPy asarray had copied object, because they need to convert it into NumPy array.

But when we use one NumPy array to create a 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 doesn't copy if not needed in contrast with array method behavior.

11. Conclusion

We learned various ways to create a NumPy array using Python List and Tuples.

Download source code related to this tutorial here

  1. Python - create-array-with-python-structures.py
  2. Jupyter - create-array-with-python-structures.ipynb

Run the Jupyter notebook for this tutorial.

12. References

  1. Various NumPy data types
  2. NumPy array(object) method
  3. NumPy array vs asarray

Comments

Popular posts from this blog

Working with request header in Jersey (JAX-RS) guide

In the  previous post , we talked about, how to get parameters and their values from the request query string. In this guide learn how to get request header values in Jersey (JAX-RS) based application. We had tested or used the following tools and technologies in this project: Jersey (v 2.21) Gradle Build System (v 2.9) Spring Boot (v 1.3) Java (v 1.8) Eclipse IDE This is a part of  Jersey (JAX-RS) Restful Web Services Development Guides series. Please read Jersey + Spring Boot getting started guide . Gradle Build File We are using Gradle for our build and dependency management (Using Maven rather than Gradle is a very trivial task). File: build.gradle buildscript { ext { springBootVersion = '1.3.0.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' a

Ajax Cross Domain Resource Access Using jQuery

Some time back in our project we faced a problem while making an Ajax call using jQuery. Chrome Browser console had given some weird error message like below when we try to access one of our web pages: When we try to access the same web page in the Firefox browser, it doesn't give any error in the console but some parsing error occurred. In our case, we were accessing XML as an Ajax request resource. I was curious to check if the non-XML cross-domain resource was successfully loading or not. But finally, I realized that it is not going through. jersey-spring-boot-quick-starter-guide In our Ajax call, requesting domain was not the same as the requested URL domain. $.ajax({ url: "https://10.11.2.171:81/xxxxxx/xxxxxxx.xml" , type : "get" , success: function (response) { alert( "Load was performed." ); }, error : function (xhr, status) {

FastAPI first shot

Setup on my Mac (Macbook Pro 15 inch Retina, Mid 2014) Prerequisite Python 3.6+ (I used 3.7.x. I recently reinstalled OS after cleaning up disk, where stock Python 2.7 was available. I installed Pyenv and then used it to install 3.7.x). I already had a git repo initialized at Github for this project. I checked that out. I use this approach to keep all the source code safe or at a specific place 😀. I set the Python version in .python-version file. I also initialize the virtual environment using pyenv in venv folder. I started the virtual environment. FastAPI specific dependencies setup Now I started with basic pip commands to install dependency for the project. I saved dependencies in requirements.txt  the file. Minimal viable code to spin an API Server FastAPI is as cool as NodeJS or Go Lang (?) to demonstrate the ability to spin an API endpoint up and running in no time. I had the same feeling for the Flask too, which was also super cool. app/main.py: from typing i