Notice: Function register_block_script_handle was called incorrectly. The asset file (/home/beaczwhx/dggulaitutorial.com/wp-content/plugins/seo-by-rank-math/includes/modules/schema/blocks/faq/assets/js/index.asset.php) for the "editorScript" defined in "rank-math/faq-block" block definition is missing. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /home/beaczwhx/dggulaitutorial.com/wp-includes/functions.php on line 6031

Notice: Function register_block_script_handle was called incorrectly. The asset file (/home/beaczwhx/dggulaitutorial.com/wp-content/plugins/seo-by-rank-math/includes/modules/schema/blocks/howto/assets/js/index.asset.php) for the "editorScript" defined in "rank-math/howto-block" block definition is missing. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /home/beaczwhx/dggulaitutorial.com/wp-includes/functions.php on line 6031

Notice: Function register_block_script_handle was called incorrectly. The asset file (/home/beaczwhx/dggulaitutorial.com/wp-content/plugins/seo-by-rank-math/includes/modules/schema/blocks/schema/assets/js/index.asset.php) for the "editorScript" defined in "rank-math/rich-snippet" block definition is missing. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /home/beaczwhx/dggulaitutorial.com/wp-includes/functions.php on line 6031
How To Create NumPy Array From List In Python - Dggul AI Tutorial

How To Create NumPy Array From List In Python


Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297
Hello coders, do you want to know how to create NumPy array from list? So in this tutorial you will learn to create numpy array from list. We will learn to create 1d, 2d, 3d and multidimensional NumPy array from list.

But before proceeding ahead you should check previous tutorial of NumPy array creation.

How To Create NumPy Array In Python

A list is a most common data structure in Python. It holds heterogenous data and it is mutable. A NumPy array can be create with many things like NumPy inbuilt functions, tuples, lists etc. But in this how to create NumPy array from list tutorial, we will learn to create NumPy array from list.

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

how to create NumPy array from list

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

how to create NumPy array from list

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

how to create NumPy array from list

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

how to create NumPy array from list

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….

 

Leave a Comment