Methods to create NumPy array from list, tuple, or list of lists in Python
In this article, we will discuss some basics of NumPy array and how to create NumPy array from lists, tuples, and Lists of lists in python.
NumPy
NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an ndimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.
numpy.array()
This method is going to be widely used in our program so it will be beneficial to study this method in advance so that it will be easy for us to understand the concepts. This method helps us to create a numpy array from already defined data structures in python like lists, tuples, and nested lists.
Syntax: numpy.array(object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0)
This method returns a numpy array.
numpy.asarray()
This method helps us to create a numpy array from already defined data structures in python like lists, tuples, and nested lists.
Syntax: numpy.asarray(arr, dtype=None, order=None)
Difference between numpy.array() and numpy.asarray()
The major difference between both the methods is that numpy.array() will make a duplicate of original copy while the numpy.asarray() make changes in the original copy.
Create a numpy array from a list

Method 1Using numpy.array()
In this method, we will see how we can create a numpy array from the list in python. When we look at the parameter of this method we see that in the parameter one parameter is an object that clearly gives us a hint that we can pass our list in this method and we may get our output in the form of NumPy array. Let us see this method with the help of an example.
l=[1,2,3,4,5,6] array=np.array(l) print(array) print(type(array))
Output
[1 2 3 4 5 6] <class 'numpy.ndarray'>
Here we see how we easily create our numpy array from the list using numpy.array() method.

Method 2Using numpy.asarray()
This method also works in the same way as numpy.array() work.We just simply pass our list object in the function and we will get our numpy array. Let see this with an example.
l=[1,2,3,4,5,6] array=np.asarray(l) print(array) print(type(array))
Output
[1 2 3 4 5 6] <class 'numpy.ndarray'>
Create a numpy array from a tuple

Method 1Using numpy.array()
Here the procedure is the same as we discuss in the case of lists. Here instead of a list, we have to pass out a tuple and we get our numpy array. Let see this with the help of an example.
t=(1,2,3,4,5,6) array=np.array(t) print(array) print(type(array))
Output
[1 2 3 4 5 6] <class 'numpy.ndarray'>

Method 2Using numpy.asarray()
Just like we use this method in the case of lists here we also use this in a similar way. But here instead of using the list, we pass tuple as an object. Let see this with the help of an example.
t=(1,2,3,4,5,6) array=np.asarray(t) print(array) print(type(array))
Output
[1 2 3 4 5 6] <class 'numpy.ndarray'>
Create a 2d Numpy array from a list of list
numpy. array() method will also work here and the best part is that the procedure is the same as we did in the case of a single list.In this case, we have to pass our list of lists as an object and we get our output as a 2d array.Let see this with the help of an example.
l=[[1,2,3],[4,5,6]] array=np.asarray(l) print(array) print(type(array))
Output
[[1 2 3] [4 5 6]] <class 'numpy.ndarray'>
So these are the methods to create numpy array from list,tuples and lists of lists.