Way to create a 1-Dimensional NumPy array using a list?

Numpy

The 1-D array is the basic array size that NumPy provides us to use in a lot of mathematical problems. Today we are going to learn how to create a 1-D array using the NumPy library. We will import NumPy to use in our program, and then we will use the NumPy methods to create a 1-D NumPy array. So let us start our program:

1. We will Create a NumPy array using a regular Python list

As you know, in Python we have a lot of data structures like lists, tuples, dictionaries etc. In our first example we will learn, how to create a NumPy array by using a regular Python list. NumPy provides us the array() function that is capable of creating a NumPy array. As an argument to this methods, We can pass the Python list to the array function and this will create an array using this list.

Here are some examples of this:

# Example-1

import numpy as np
array_one = np.array([1,2,3,4])
print("array one is linear",array_one)

Output

array one is linear [1 2 3 4]

# Example-2

numbers = [1,2,3,4]
array_two = np.array(numbers)
print("array two is linear",array_two)

Output:

array two is linear [1 2 3 4]

These are some of the simple examples which we have taken. In practical data science cases we read huge data files and get the data in the form of Python lists.

Those lists can be converted to numpy array to perform the calculations.