-2

without knowing the 'size' how to take multi-line input and store it in a list ?

1
2
3
4  

>   list=[]
>   size=int(input())
>   for i in range(size):
>      list.append(int(input()))
gagandeep
  • 45
  • 8
  • 1
    How do you know when you've reached the end of the input? – glibdud Sep 29 '17 at 17:01
  • Assuming keyboard input: you read one line at a time, each line is terminated by Enter. You'll need a stop condition, for example an empty input. – Stefan Sep 29 '17 at 17:02
  • list=[int(x) for x in input.split()] , i want something like this here also end of input is not specified. – gagandeep Sep 29 '17 at 17:03
  • `input.split()` doesn't make sense: `AttributeError: 'builtin_function_or_method' object has no attribute 'split'` – PM 2Ring Sep 29 '17 at 17:05
  • If you want to read _everything_ you can read `sys.stdin`, but the user will need to send the End of File code, on Unix-like systems you can do that with Ctrl-D. – PM 2Ring Sep 29 '17 at 17:08
  • If end of input is not specified, then how do you know when to stop reading? – glibdud Sep 29 '17 at 17:18
  • @i-Gagan i updated my answer – amarynets Sep 29 '17 at 17:20
  • You are contradicting yourself... First you say that the length of the input is not specified, but your example code seems to expect the length of the input as first value. Then you say that you don't want to use list comprehensions, but then show a list comprehension as your desired code. – mkrieger1 Sep 29 '17 at 17:23
  • @mkrieger1 , 'examle code' and 'list comprehensions' are the two ways that i know and they both are not working here. i need to take input till a blank line appears. – gagandeep Sep 29 '17 at 17:30
  • See https://stackoverflow.com/questions/20511159/accepting-input-till-newline-in-python or https://stackoverflow.com/questions/20337489/python-how-to-keep-repeating-a-program-until-a-specific-input-is-obtained – mkrieger1 Sep 29 '17 at 17:48
  • 2
    "i need to take input till a blank line appears. " You **really** should have mentioned that in your question! – PM 2Ring Sep 30 '17 at 05:19

1 Answers1

0
result = []
while True:
    item = input()
    if not item:
        break
    result.append(int(item))
amarynets
  • 1,765
  • 10
  • 27