Open In App

How to Take Array Input in Python Using NumPy

Last Updated : 19 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

NumPy is a powerful library in Python used for numerical computing. It provides an efficient way to work with arrays making operations on large datasets faster and easier. To take input for arrays in NumPy, you can use numpy.array.

Taking Array Input Using numpy.array()

The most simple way to create a NumPy array is by converting a list of inputs into an array. It works well for scenarios where all elements are provided at once in a single line.

Python
import numpy as np

# Take input for the array as a space-separated string

val = input("Enter elements separated by space: ").split()

# Convert input elements to a NumPy array
# Convert to integer array, change dtype as needed

arr = np.array(val, dtype=int)

print(arr)

Output:

Enter elements separated by space: 1 2 3 4 5 
[1 2 3 4 5]

Explanation: Here, we first take input as a space-separated string split it into a list and then use numpy.array() to convert it into a NumPy array.

Let's explore other methods to create a NumPy array from user input:

Using List Comprehension with NumPy

We can also use list comprehension to create a NumPy array from user input. We can provide prompts for each input which makes it suitable for user-friendly input handling.

Python
import numpy as np

# Take input for the number of elements
n = int(input("Enter the number of elements: "))

# Use list comprehension to collect elements
val = [int(input(f"Enter element {i + 1}: ")) for i in range(n)]

# Convert to NumPy array
arr = np.array(val)
print(arr)

Output:

Enter the number of elements: 6
Enter element 1: 23
Enter element 2: 45
Enter element 3: 21
Enter element 4: 45
Enter element 5: 23
Enter element 6: 12
[23 45 21 45 23 12]

Explanation: This code uses list comprehension to take n elements and then converts the resulting list into a NumPy array.

Using numpy.fromiter()

Another way to create a NumPy array is by using numpy.fromiter() which creates an array from an iterable. This method creates arrays directly without creating an intermediate list.

Python
import numpy as np

# Take input as space-separated values

val = input("Enter elements separated by space: ").split()

# Use fromiter() to create a NumPy array

arr = np.fromiter((int(x) for x in val), dtype=int)

print(arr)

Output:

Enter elements separated by space: 12 18 87 83 86
[12 18 87 83 86]

Explanation: Here, we use a generator expression to create an iterable that numpy.fromiter() can convert to a NumPy array.

This method is suitable for processing large data inputs where conversion speed and efficiency matter.


Taking Input for Multi-dimensional Arrays

For multi-dimensional arrays, you can take input for each row separately and stack them.

Python
import numpy as np

# Input number of rows and columns
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))

# Empty list to hold the rows
data = []

# Input each row
for i in range(rows):
    row = list(map(int, input(f"Enter row {i + 1} elements separated by spaces: ").split()))
    data.append(row)

# Convert list of lists to NumPy array
arr = np.array(data)
print("2D NumPy Array:")
print(arr)

Output:

Enter the number of rows: 2
Enter the number of columns: 3
Enter row 1 elements separated by spaces: 1 2 3
Enter row 2 elements separated by spaces: 4 5 6
2D NumPy Array:
[[1 2 3]
[4 5 6]]




Next Article
Practice Tags :

Similar Reads