Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

geting input in one line array / list

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

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

Continue reading