In this post, we are going to explore How to Create 2D NumPy Array from list of lists that include 1D,2D,3D arrays from a list or lists of a list, a tuple with code examples using the NumPy library.
What is numpy.array()
The Python NumPy module is mainly used with arrays manipulation, array objects in Numpy know as ndarray. The NumPy library array() method is used to create an array ndarray from sequences like list, lists of the list, tuple or array_like object.
To use the NumPy library, We first need to install the NumPy library using the below pip command after installation we can use it in our program.
#install NumPy
Pip install numpy
#To use import Numpy using
import numpy as np
Let us understand how to create different dimensions of NumPy array using np.array() method.
1. Convert list to 1D NumPy array
In this example, we are passing a list of elements to create a 1D NumPy array.
import numpy as np
array_one = np.array([14,15,16,17,18,23])
print("one dimesnional NumPy array from list :",array_one)
Output
one dimesnional NumPy array from list : [14 15 16 17 18 23]
2. Create 1D NumPy Array from tuple
In this code example, we are passing a tuple to np.array() method to create a 1D NumPy array from the tuple.
import numpy as np
tuple = (14,56,16,21,78,25)
np_array = np.array(tuple)
print("Shape: ", np_array.shape)
print("NumPy 1D array from Python Tuple :\n",np_array)
Output
Shape: (6,)
NumPy 1D array from Python Tuple :
[14 56 16 21 78 25]
3. Create a NumPy array from a List of tuples
In this example, we are Creating a Numpy array from a List of tuples.
import numpy as np
tuple = (14,56,16,21,78,25)
np_array = np.array(tuple)
print("Shape: ", np_array.shape)
print("NumPy array from list of Tuples :\n",np_array)
Output
Shape: (3, 3)
NumPy array from lsit of Tuples
[[14 56 16]
[21 78 25]
[10 27 15]]
4. Convert Lists of list to 1D NumPy array
In this code example, we are passing a lists of list to np.array() method to create 1D NumPy array from lists of list.
import numpy as np
lists_of_list = [[1.4,5.6,16], [21,7.8,25]]
np_array = np.array([ elem for oneDarray in lists_of_list for elem in oneDarray])
print("Shape: ", np_array.shape)
print("NumPy 1D array from Python lists of list :\n",np_array)
Output
Shape: (6,)
NumPy 1D array from Python lists of list :
[ 1.4 5.6 16. 21. 7.8 25. ]
Let us understand how to create 2D NumPy array using np.array()
5. Convert list of lists to 2 D NumPy array
In this code example, we are passing a lists of list to np.array() method to create 2D NumPy array from lists of list.
import numpy as np
np_array = np.array([[14,15,16,17],[21,23,25,26],[31,32,33,34]])
print("Shape(rows,columns): ", np_array.shape)
print("Two dimesnional NumPy array from list :\n",np_array)
Output
Shape(rows,columns): (3, 4)
Two dimesnional NumPy array from list :
[[14 15 16 17]
[21 23 25 26]
[31 32 33 34]]
6. Create 2D Numpy Array from mixed datatype Lists of list
In this code example, we are passing a lists of list of mixed datatypes to np.array() method to create 2D NumPy array from lists of list.
import numpy as np
lists_of_list = [[1.4,5.6,16,'C#'], [21,7.8,25,'C++']]
np_array = np.array(lists_of_list)
print("Shape: ", np_array.shape)
print("create NumPy array from mixed datatype Python list :\n",np_array)
Output
Shape: (2, 4)
create NumPy array from mixed datatype Python list :
[['1.4' '5.6' '16' 'C#']
['21' '7.8' '25' 'C++']]
7. Convert 3D list to NumPy array
We can pass the list to the array function and this will create an array using this list.
import numpy as np
lists_of_list = [
[[14,15,16], [21,23,25], [31,32,33]],
[[41, 42, 43], [51, 52, 53], [61, 62,63]],
]
np_array = np.array(lists_of_list)
print("Shape(rows,columns): ", np_array.shape)
print("Three dimensional NumPy array from list :\n",np_array)
Output
Shape(rows,columns): (2, 3, 3)
Three dimensional NumPy array from list :
[[[14 15 16]
[21 23 25]
[31 32 33]]
[[41 42 43]
[51 52 53]
[61 62 63]]]
8.Convert list of list NumPy array and pass datype
We can pass the datatype as a second argument and can create a float 2-D array. We can pass any data type like: ‘float’, ‘int’, ‘bool’, ‘str’ and ‘object’
In this below example we are passing ‘int’ as a datatype to create a NumPy array of integer type.
import numpy as np
lists_of_list = [[1.4,5.6,16], [21,7.8,25], [31,9.8,33]]
np_array = np.array(lists_of_list,np.int)
print("Shape: ", np_array.shape)
print("pass datatype to create NumPy array from list :\n",np_array)
Output
Shape: (3, 3)
pass datatype to create NumPy array from list :
[[ 1 5 16]
[21 7 25]
[31 9 33]]
9.Convert NumPy array into different type
We can Convert the datatype of each element to ‘str’ using astype() function.We can pass any datatype like : ‘float’, ‘int’, ‘bool’, ‘str’ and ‘object’
In this below example we are converting NumPy array to ‘string’ and float datatypes
import numpy as np
lists_of_list = [[14,56,16], [21,78,25], [31,98,33]]
np_array = np.array(lists_of_list)
print("Shape: ", np_array.shape)
print(" created original NumPy array from list :\n",np_array)
print("\n Convert NumPy array to string :",np_array.astype('str'))
print("\n Convert NumPy array to float :",np_array.astype('float'))
Output
Shape: (3, 3)
created original NumPy array from list :
[[14 56 16]
[21 78 25]
[31 98 33]]
Convert NumPy array to string : [['14' '56' '16']
['21' '78' '25']
['31' '98' '33']]
Convert NumPy array to float : [[14. 56. 16.]
[21. 78. 25.]
[31. 98. 33.]]