how to get input like this in python3
first input is = 2
and based on first input get new inputs
for example:
2 # how many inputs?
1 2 # 2 numbers inputs
or
3 # how many inputs?
3 5 8 # in one line getting 3 inputs
here another one:
4
6 8 7 9
how I can get in one line inputs based on first inputs?
i tried to use map but no luck
n = int(input())
for i in range(n):
x = map(int,input().split())
but the output is
if n is 2
1 2
again asking for inputs because of i = 0 getting 1 2 and i = 1 again aksing for input
how I can get in one line inputs based on first inputs like list?
>Solution :
The reason is that you’re running a loop n times and in each iteration you are picking up a whole line of input. Thus, at the end x contains the latest input line.
Instead you can read space separated input and store it into list directly.
n = int(input())
x = list(map(int,input().split()))
Although, if you expect that second line can contain more than n numbers, you can pick up first n numbers through
n = int(input())
x = list(map(int,input().split()))[:n]