The Wayback Machine - https://web.archive.org/web/20230512160425/https://www.geeksforgeeks.org/creating-a-one-dimensional-numpy-array/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Creating a one-dimensional NumPy array

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

One dimensional array contains elements only in one dimension. In other words, the shape of the NumPy array should contain only one value in the tuple. Let us see how to create 1 dimensional NumPy arrays.

Method 1: First make a list then pass it in numpy.array()

Python3




# importing the module
import numpy as np
 
# creating the list
list = [100, 200, 300, 400]
 
# creating 1-d array
n = np.array(list)
print(n)

Output:

[100 200 300 400]

Method 2: .fromiter() is useful for creating non-numeric sequence type array however it can create any type of array. Here we will convert a string into a NumPy array of characters.

Python3




# importing the module
import numpy as np
 
# creating the string
str = "geeksforgeeks"
 
# creating 1-d array
x = np.fromiter(str, dtype = 'U2')
print(x)

Output:

['g' 'e' 'e' 'k' 's' 'f' 'o' 'r' 'g' 'e' 'e' 'k' 's']

Method 3: .arange() returns evenly spaced values within a given interval.

Python3




# importing the module
import numpy as np
 
# creating 1-d array
x = np.arange(3, 10, 2)
print(x)

Output:

[3 5 7 9]

Method 4: .linspace() creates evenly space numerical elements between two given limits.

Python3




# importing the module
import numpy as np
 
# creating 1-d array
x = np.linspace(3, 10, 3)
print(x)

Output:

[ 3.   6.5 10. ]

Method 5: .zeros() returns the numbers of 0s as passed in the parameter of the method 

Python3




import numpy as np
 
arr5 = np.zeros(5)
print(arr5)

Output:

[0. 0. 0. 0. 0.]

Method 6: .ones() returns the numbers of 1s as passed in the parameter of the method 

Python3




import numpy as np
 
arr6 = np.ones(5)
print(arr6)

Output:

[1. 1. 1. 1. 1.]

My Personal Notes arrow_drop_up
Last Updated : 07 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials