Skip to content

How to take integer array input in Python | Example code

  • by

It’s very easy to take array input in Python. Use input() function with map and split() functions.

Example take array input in Python

Simple example code.

If the numbers are provided in the same line then you can use them using a map().

arr = list(map(int, input().split()))

print(arr)

Output:

How to take integer array input in Python

If inputs are in different lines then, iterating till the range. Where enter N and then take N number of elements.

n = int(input('Enter how many Array elements: '))

arr = [int(input()) for i in range(n)]

print(arr)

Output:

Enter how many Array elements: 5
1
2
3
4
5
[1, 2, 3, 4, 5]

Do comment if you have any doubts or suggestions on this Python input topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading