Convert a Python array into a Numpy array


Array is one of the data structures which allows us to store the same data type elements in a contiguous block of memory. Arrays can be in one dimension or two dimension or three dimension up to 32 dimensions.

In python, there are different ways to create arrays. One way is by using the built-in module array which allows us to create the array with different data types like integers and floats. The other way is by using the Numpy library, which provides most powerful and flexible functions to implement the arrays.

Creating array using array module

The built in module in python is the array, which helps us to create the arrays in different dimensions.

Syntax

The following is the syntax for using the array module.

import array as arr
arr.array(datatype,list)

Where,

  • Array is the built-in module in python.

  • arr is the alias name of array module.

  • List is the list of all the elements.

Example

In the following example, we are creating the array by passing a list of elements to the array() function of the array module, along with the desired data type. This function will return an array with the specified elements.

import array as arr
array_list = [22,24,14,12,7,2,1,5,21,11]
print("The list of elements to be used in array:",array_list)
created_arr = arr.array('i',array_list)
print("The created array using the specified list of elements:",created_arr)
print(type(created_arr))

Output

The list of elements to be used in array: [22, 24, 14, 12, 7, 2, 1, 5, 21, 11]
The created array using the specified list of elements: array('i', [22, 24, 14, 12, 7, 2, 1, 5, 21, 11])

Example

Another way to create the multidimensional array is by using the array function of the array module.

import array
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr = array.array('i', [elem for sublist in data for elem in sublist])
rows = len(data)
cols = len(data[0])
for i in range(rows):
    for j in range(cols):
        print(arr[i*cols + j], end=' ')
    print()

Output

1 2 3 
4 5 6 
7 8 9 

Creating array using the Numpy library

Numpy library, provides array() function which helps us to create the arrays in different dimensions.

Syntax

Following is the syntax for using the Numpy library.

numpy.array(list)

Where,

  • Numpy is the library.

  • Array is the function which creates the array.

  • List is the list of elements.

Example

In the following example, we will create a 1-d array using the array() function of the numpy library by passing the list of elements as an argument.

import numpy as np
list_elements = [232,34,23,98,48,43]
print("The list of elements to be used to create the array:",list_elements)
arr = np.array(list_elements)
print("The created array:",arr)
print("The dimension of the array:",np.ndim(arr))

Output

The list of elements to be used to create the array: [232, 34, 23, 98, 48, 43]
The created array: [232 34 23 98 48 43]
The dimension of the array: 1

Example

Let’s see another example which creates the 2-d array by passing the list of elements to the array() function of the numpy library.

import numpy as np
list_elements = [[3,4],[2,3]]
print("The list of elements to be used to create the array:",list_elements)
arr = np.array(list_elements)
print("The created array:",arr)
print("The dimension of the array:",np.ndim(arr))

Output

The list of elements to be used to create the array: [[3, 4], [2, 3]]
The created array: [[3 4]
[2 3]]
The dimension of the array: 2

Updated on: 09-Aug-2023

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements