0

I have a simple question, I just want to store an input of multiple lines into an array using python, note that the first line of the input is telling me how many lines there will be, so if the first line of the input is 4, the whole input will be of 5 lines in total.

Example:

input:

4
1
2
3
4

output:

[1, 2, 3, 4]

I tried using n = list(map(int, input())) however when I print the whole list it only stores the first line of the input, and I need all the values.

Thank you.

6
  • How many lines need to be input? Is it 5 everytime you run the program? Commented May 24, 2020 at 20:48
  • input() will be called only once this way, you will need some kind of an explicit loop for this. Commented May 24, 2020 at 20:48
  • 1
    The integer of the first line tells me how many lines the input will have below that line, so if the first line is 5, that means the input will have 6 lines, I did not considere that as important so I am going to modify my question to be more clear. Commented May 24, 2020 at 20:52
  • 1
    That was also an important input, to solve/explain any problem Don't miss any information for solving the question Commented May 24, 2020 at 20:55
  • 2
    Okay, you do understand that input() reads a single line from the standard input, yes? So of course anything you do with a single line of input will only process that line of input. You need to come up with a step-by-step process that handles multiple lines of input. Yes? So, think it through logically. What is the first step in this process: you read the first line, and it tells you how many more lines to process. Yes? So implement that. Now that you have that number, do you see how to proceed with the rest of the input? Commented May 24, 2020 at 21:06

6 Answers 6

3

Use a list comprehension, calling the input() function for each iteration, like so:

l = [int(input()) for _ in range(int(input()))]

output for print(l), with the first input being 4:

[1, 2, 3, 4]
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer, but note that my program does not know how many lines the input will have until running time, the first line of the input tells me how many lines the total input have plus one (see my question again), I didn't considere that as important so I did not include it on my question, now I've modified it.
@Tomerikoo What do you think about my answer, is it fine this time ? or it's better to use list comprehension ?
The input is 4, that means that below that line, there will be 4 lines, so in total, the first line integer + 1 tells me how many lines the total input will have.
2

Updated answer based on comment discussion.

num = input()
li = []
for _ in range(num):
    data = int(input())
    li.append(data)

print(li)

input

4
6
3
9
4

output

[ 6 , 3 , 9 , 4 ]

Comments

1

I don't know if this solution is too basic

output = []
tot = int(input())
for i in range(tot):
    num=int(input())
    output.append(num)

print(output)

If I input 4\n5\n6\n7\n2\n, I get the output:

[5, 6, 7, 2]

So, basically the first input gets the number of following inputs, and it is used to calculate the range of a for-loop, in which, for every iteration, an input and a list append is performed.

Please note how every input is returned in string format and needs to be converted into an integer.

Comments

1

This should work fine, looping in specific range and appending into a new list the inputs.

new_list = []
number_of_loop = int(input())
for _ in range(number_of_loop):
    new_list.append(int(input()))

Output of print(new_list), if the number_of_loop was 5:

[1, 1, 1, 1, 1]

2 Comments

Thank you for your answer, but note that my program does not know how many lines the input will have until running time, the first line of the input tells me how many lines the total input have plus one (see my question again), I didn't considere that as important so I did not include it on my question, now I've modified it.
@Santiago Updated
0

input() reads whole single line. You can extend it using sys.stdin.

Now, In your case, each line contains a single integer so there can be two cases:

  • When you are given the number of integers to be read into the list: You can loop through multiple lines and read using int(input()) and append it to the list. (This is the your actual case):
#python 3.x
n = int(input())
ls = []
for i in range(n):
   ls.append(int(input())

or

#python 3.x
import sys
n = int(input())
ls = list(map(int,sys.stdin.read().strip().split()))

if your input is like this

5
1
2
3
4
5

then value of n and ls will be

n = 5
ls = [1, 2, 3, 4, 5]
  • When you are not given the number of integers to be read into the list or don't know the size of list:
#python 3.x
import sys
ls = list(map(int, sys.stdin.read().strip().split())) #reading whole input from stdin then splitting

if your input is like this

1
2
3
4
5

then value of ls will be

ls = [1, 2, 3, 4, 5]

Comments

0
description = []
while input() != "n":
   line = input()
   description.append(line)
   input("Do you want to continue (y/n)")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.