Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Creating a one-dimensional NumPy array

  • Last Updated : 02 Sep, 2020

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()

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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




# imporint gthe 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. ]



My Personal Notes arrow_drop_up
Recommended Articles
Page :