 # Numpy asarray Implementation in Python with Examples

Numpy asarray allows you to convert python data structures into NumPy array. You can convert list, list of list, tuple, or list of tuples into NumPy array. We convert python data structure to NumPy array for fast computation and to increase efficiency. In this entire tutorial, you will know the various examples of using numpy.asarray() in python.

## Examples on Numpy asarray()

In this section, you will know how to use and convert list, tuples e.t.c and to convert it to NumPy array.

### Example 1: Converting a list to a Numpy array

Suppose I have a python list and want to convert it into a NumPy array. Then I have to pass the list as an argument to the numpy.asarray() method. Execute the below lines of code.

``````import numpy as np
python_list = [1,2,3,4,5]
list_to_numpy_arr = np.asarray(python_list)
print(list_to_numpy_arr)
print(type(list_to_numpy_arr))``````

Output

You can see the output type of the list is the NumPy array.

### Example 2: Convert List of the lists to Numpy array

Now, let’s convert a list of lists to a NumPy array. In the above example, you have got a single-dimensional NumPy array. But here you will get a two-dimensional NumPy array. Let’s execute the code and see the output.

``````import numpy as np
python_list_of_list = [[1,2],[3,4],[5,6]]
list_of_list_to_numpy_arr = np.asarray(python_list_of_list)
print(list_of_list_to_numpy_arr)
print(type(list_of_list_to_numpy_arr))``````

Output

You can see the output of the list of lists is the multi-dimensional NumPy array.

### Example 3: Numpy array from tuples using asarray

In the above example I have converted list or list of list to NumPy array, lets apply the NumPy asarray() method on tuples. Just execute the given lines of code and see the output.

``````import numpy as np
python_tuple = (10,20,30,40,50)
tuple_to_numpy_arr = np.asarray(python_tuple)
print(tuple_to_numpy_arr)
print(type(tuple_to_numpy_arr))``````

Output

### Example 4: Converting a list of tuples to a Numpy array

In this last example, I will convert the list of tuples to the NumPy array. Here the output will the multi-dimensional array. Copy, paste, and execute the code.

``````import numpy as np
python_list_of_tuple = [(10,20),(30,40),(50,60)]
list_of_tuple_to_numpy_arr = np.asarray(python_list_of_tuple)
print(list_of_tuple_to_numpy_arr)
print(type(list_of_tuple_to_numpy_arr))``````

You will get the following output.

Output

## END NOTES

Numpy asarray allows you to convert any simple data structures into NumPy array. After the conversion, you can easily manipulate the output array. These are the examples I have aggregated for you. I hope these examples have solved your queries. If you want to get more information on it then you can contact us for more help.

Source:

Numpy asarray Documentation 