Python add items seperated by newlines to list from terminal


I am trying to take an input formatted such as this (i.e values/items seperated by newlines) which is pasted into the terminal:


And have them all taken as an input and then appended to a list:

["a", "b", "c"]

While this seems like it should be very easy, I am having trouble figuring it out. If I try to paste the 3 items at once into the terminal only the first 1 is taken as the input. I end up with my list being just ["a"]and the others are ignored or taken as commands, which I do not want.

I did this:

ls = []

while len(ls) < 3:
    items = input()


Which works if I have 3 items, however the number of items in my use case will be unkown, hence I cannot rely on this. Any help or critique, or a link to a previously answered question would be welcome.

Asked By: mihkelr



The problem is that you don’t know when the user will want to stop inputting data. An easy solution is to continue looping until the user doesn’t enter any input. Like this:

ls = []

while True:
    items = input()
    if items == "":


As long as the user inputs anything, the loop will continue. But when the user presses enter on a blank line, the loop will stop and the rest of the program will continue.

Answered By: Michael M.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.