-1

I want to read an array of integers from single line where size of array is given in python3.
Like read this to list.

5 //size

1 2 3 4 5  //input in one line

while i have tried this

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

but dont succeed how to give size.

Please help
I am new to python 3

Janardhan Singh
  • 447
  • 5
  • 12

2 Answers2

1

Assuming you got the array size before,

you do this first:

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

then you slice it

print(arr[:size])
farbiondriven
  • 2,450
  • 2
  • 15
  • 31
1

This appears to be for a coding competition site like hackerrank or codechef. First you need to read the first line line with the size. Then read the second line with the data. In python, you don't need to know the size of the data before reading it, so get the input and discard it. Some other languages, you need to know the size of input data in order to read it in properly.

array_size = int(input())
arr = [map(int, input().split())][:array_size]
Alan Hoover
  • 1,430
  • 2
  • 9
  • 13