Create One Dimensional Array - NumpPy Examples


NumPy - Create 1D Array

A one-dimensional array contains elements in a single dimension. In other words, the shape of the NumPy array has only one value in the tuple (e.g., shape=(n,)).

To create a one-dimensional array in NumPy, you can use one of the following functions:

  1. numpy.array() for converting a list into a NumPy array.
  2. numpy.arange() for creating arrays with a specified range of values and intervals.
  3. numpy.linspace() for creating arrays with a specified range and a defined number of elements.

Let's go over each function with examples.


1. Create 1D NumPy Array using array() function

The numpy.array() function is used to create a NumPy array from an existing Python list. It is the simplest method for creating a one-dimensional array.

In this example, we will import the numpy library and use the array() function to create a one-dimensional NumPy array from a list of numbers.

Python Program

import numpy as np

#create numpy array
a = np.array([5, 8, 12])
print(a)

Output

[ 5  8 12]

This creates a simple array with elements 5, 8, 12.


2. Create 1D NumPy Array using arange() function

The numpy.arange() function is used to generate a sequence of numbers with a specified start, stop, and step (interval) value. It is ideal for creating arrays with evenly spaced numbers.

In this example, we will use arange() to create a one-dimensional array that starts at 5, ends before 14, and increments by 2.

Python Program

import numpy as np

#create numpy array
a = np.arange(5, 14, 2)
print(a)

Output

[ 5  7  9 11 13]

The array starts at 5 and continues up to 13, incrementing by 2.

You can also create an array that includes only even or odd numbers or fits any other sequence you need, by adjusting the start, stop, and step parameters.


3. Create 1D NumPy Array using linspace() function

The numpy.linspace() function is used to create a one-dimensional array with a specified number of elements, evenly spaced between a start and end value. This is useful when you want to divide a range into a specific number of intervals.

In this example, we will use linspace() to create a one-dimensional array with 4 elements between 5 and 25.

Python Program

import numpy as np

#create numpy array
a = np.linspace(5, 25, 4)
print(a)

Output

[ 5.         11.66666667 18.33333333 25.        ]

The array contains 4 evenly spaced values between 5 and 25.

The number of elements in the array is determined by the third parameter in linspace().


4. Create 1D NumPy Array with Negative Values using arange() function

To create a sequence of negative values, you can use arange() by specifying a negative step size. This can be useful when you want to create arrays counting down or between negative and positive values.

Python Program

import numpy as np

#create numpy array with negative values
a = np.arange(-5, 0, 1)
print(a)

Output

[-5 -4 -3 -2 -1]

This creates an array of integers from -5 to -1.


Summary

In this tutorial of NumPy Tutorial, we explored how to create one-dimensional NumPy arrays using different NumPy functions:

  • numpy.array() for creating arrays from lists.
  • numpy.arange() for generating sequences with specified intervals.
  • numpy.linspace() for creating arrays with evenly spaced numbers between a start and end.

We also covered how to create arrays with negative values using arange() and additional use cases such as specifying the number of elements with linspace().


Python Libraries