w3resource logo


Python Exercises

Python NumPy: Convert a list of numeric value into a one-dimensional NumPy array

Python NumPy: Array Object Exercise-2 with Solution

Write a Python program to convert a list of numeric value into a one-dimensional NumPy array.

Sample Solution:-

Python Code:

import numpy as np
l = [12.23, 13.32, 100, 36.32]
print("Original List:",l)
a = np.array(l)
print("One-dimensional numpy array: ",a)

Sample Output:

Original List: [12.23, 13.32, 100, 36.32]                               
One-dimensional numpy array:  [  12.23   13.32  100.     36.32]

numpy.array: Create an array.

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


Name Description Required / Optional
object An array, any object exposing the array interface. Required
dtype : data-type, optional The desired data-type for the array. Optional
copy (bool) If true (default), then the object is copied. Optional
order : ‘K’, ‘A’, ‘C’, ‘F’ Specify the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless ‘F’ is specified, in which case it will be in Fortran order. Optional
subok (bool) If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array. Optional
ndmin (int) Specifies the minimum number of dimensions that the resulting array should have. Optional

Python Code Editor:

Improve this sample solution and post your code through Disqus