Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297
But before proceeding ahead you should check previous tutorial of NumPy array creation.
How To Create NumPy Array In Python
How To Make NumPy Array From List
We can create NumPy array from list using two methods array() and asarray().
So let’s start to learn to create NumPy array from list.
Create NumPy Array Using array() method
We can create NumPy array from list using array() method in the following ways.
1d NumPy array from list
Run the following code to create one dimensional NumPy array from list.
import numpy as np list = [3,4,5,6] array = np.array(list) array
- First of all we have imported NumPy module.
- Then created a list.
- And using array() method created NumPy array from list. We have passed list as argument.
- Then finally printed the array.
Output
2d NumPy Array From List
import numpy as np list = [[3,4,5],[90,80,30]] array = np.array(list) array
- Here we have created a nested list of same size.
- And then passed this list to the array() method. It will create 2d NumPy array.
Output
3d NumPy Array From List
To create NumPy 3d array from list, we have to pass 2d array as arguments to the array() method. So let’s see how to do that.
import numpy as np array = np.array([[[3,4,5],[90,80,30]],[[6,4,8],[8,9,5]]]) array
Let’s see its output.
Output
Create NumPy Array From List Of Tuples
import numpy as np list_of_tuples = [(3,4,5,5),(90,80,30,2)] array = np.array(list_of_tuples) array
- Here we have created a list of tuples and passed it to the array() method.
- This will convert the list of tuples into NumPy array.
Output
Create NumPy Array From List Of Strings
import numpy as np array = np.array(["mango","banana","grapes"]) array
- Here we have passed a list of strings.
Output
Create a NumPy Array From a List With Different Data Type
To create NumPy array from list with different data type, we have to specify dtype argument in the array() method.
import numpy as np list = [[3,4,5],[90,80,30]] array = np.array(list,dtype=float) array
- Here we have specified float data type.
Output
Create NumPy Array Using asarray() method
So in this section we will learn creating NumPy array using asarray() method.
asarray() method is used to convert the given input into an array. So let’s start.
Create NumPy Array From List
import numpy as np list = [3,4,8,9,13] array = np.asarray(list) array
- Here we have created a list and passed into the asarray() method.
- It will convert the list into an array.
Output
Create NumPy Array From List Of Lists
import numpy as np list = [[3,4,8],[8,9,2],[2,1,0],[1,90,10]] array = np.asarray(list) array
- Here we have passed list of lists to create array.
Output
Lets check it’s dimension.
array.shape
Output
Create NumPy Array From List Of Tuples
import numpy as np list = [('Water','Mountains','Waterfallas'),('Mango','Banana','Grapes'),('Monkey','Lion','Tiger')] array = np.asarray(list) array
Output
So guys it was all about how to create NumPy array from list tutorial. I hope you have found it helpful, if yes then make sure to share it with your fellows who are learning NumPy. And if you have any queries regarding this tutorial then feel free to ask in comment. THANKS
People Are Also Reading….
- How To Create Dataframe In Python Using Pandas
- How To Read And Write CSV File In Python Pandas
- 5 Best Laptop For Data Science Students & Professionals 2021 | Reviews
- How To Create Pandas Series | Pandas Tutorial
- How To Install Jupyter Notebook In Windows
- 7 Best Books For Artificial Intelligence And Machine Learning For Beginners