np.array() : Create Numpy Array from list, tuple or list of lists in Python

In this article we will discuss how to create a Numpy Array from a sequence like list or tuple etc. Also, how to create a 2D numpy Numpy Array from nested sequence like lists of lists.

np.array() – Creating 1D / 2D Numpy Arrays from lists & tuples in Python

To install the python’s numpy module on you system use following command,

pip install numpy

To use numpy module we need to import it i.e.

import numpy as np

numpy.array()

Python’s Numpy module provides a function numpy.array() to create a Numpy Array from an another array like object in python like list or tuple etc or any nested sequence like list of list,

numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)

Arguments:  

  • object is an array like object i.e. list or tuple or any nested sequence like list of list.
  • dtype: (Optional) Data type of elements
  • Other parameters are optional and has default values.

Returns:

  • It returns a Numpy Array .

Let’s use this numpy.array() to create Numpy Array objects,

Create Numpy Array from a list

To create a Numpy Array from list just pass the list object to numpy.array() i.e.

# Create ndArray from a list
npArray = np.array([1,2,3,4,5,6,7,8,9])

print('Contents of the ndArray : ')
print(npArray)

Output:

[1 2 3 4 5 6 7 8 9]

Read More,

Create Numpy Array from a tuple

Similar to above example, we can directly pass the tuple to the numpy.array() to create a Numpy Array object,

# Create ndArray from a tuple
npArray = np.array( (11,22,33,44,55,66,77,88 ) )

print('Contents of the ndArray : ')
print(npArray)

Output:

Contents of the ndArray : 
[11 22 33 44 55 66 77 88]

Related Queries:

Check type of Numpy Array object

We can also check the type of the created Numpy Array using type() function i.e.

type(npArrObject)

Example:

npArray = np.array( (11,22,33,44,55,66,77,88 ) )
print(type(npArray))

Output

<class 'numpy.ndarray'>

Check the data type of elements in Numpy Array

Numpy array Numpy Array has a member variable that tells about the datatype of elements in it i.e. ndarray.dtype.

We created the Numpy Array from the list or tuple. While creation numpy.array() will deduce the data type of the elements based on input passed.
But we can check the data type of Numpy Array elements i.e.

print('Data Type of elements in  ndArray : ')
npArray = np.array((11, 22, 33, 44, 55, 66, 77, 88))
print(npArray.dtype)

Output:

int32

Create 2D Numpy Array from a list of list

Suppose we want to create 2D Numpy Array like Matrix, we can do that by passing a nested sequence in numpy.array() i.e. list of list.
For example,

# Create 2D ndarray form list of list
npArray = np.array( [ [77, 88, 99] , [31,42,63] , [11,22,33]])

print('Contents of the ndArray : ')
print(npArray)

Output:

[[77 88 99]
 [31 42 63]
 [11 22 33]]

Create 1D Numpy Array from list of list

On passing a list of list to numpy.array() will create a 2D Numpy Array by default. But if we want to create a 1D numpy array from list of list then we need to merge lists of lists to a single list and then pass it to numpy.array() i.e.

listOfLists = [[77, 88, 99], [31, 42, 63], [11, 22, 33]]

# Create one dimension ndArray from a list of lists
npArray = np.array([ elem for singleList in listOfLists for elem in singleList])

print('Contents of the ndArray : ')
print(npArray)

Output:

Contents of the ndArray : 
[77 88 99 31 42 63 11 22 33]

Create a Numpy Array from a list with different data type

We can also pass the dtype as parameter in numpy.array(). In that case numpy.array() will not deduce the data type from passed elements, it convert them to passed data type.

For example pass the dtype as float with list of int i.e.

# Create ndArray of float datatype from a list of int
npArray = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9] , dtype=float)

print('Contents of the ndArray : ', npArray)
print('Type of the ndArray : ', npArray.dtype)

Output:

Contents of the ndArray :  [1. 2. 3. 4. 5. 6. 7. 8. 9.]
Type of the ndArray :  float64

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top