Python informer

Improve your Python coding skills

Creating numpy arrays from existing data

This article is part of a series on numpy.

In this section we will look at how to create numpy arrays from existing data.

From an existing sequence

An easy way to create an array based on data is to use thearray function:

import numpy as np

k1 = [1, 3, 5, 7]
d1 = np.array(k1)
print(d1)

This creates a numpy array d1 based on the list k1:

[1 3 5 7]

You can do this for more dimensions:

k2 = [[10, 20, 30], [40, 50, 60]]
d2 = np.array(k2)
print(d2)

giving:

[[10 20 30]
 [40 50 60]]

array will accept any sequence, or numpy array, or anything that behaves like a numpy array.

Visit the PythonInformer Discussion Forum for numeric Python.