Skip to content
Related Articles

Related Articles

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[3 5 7 9]

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[ 3.   6.5 10. ]

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :