Home > database >  Create list object from multiline input with only the standard python library
Create list object from multiline input with only the standard python library

Time:12-12

I'm trying to store this single input:

5 2 100
2
8
1
3

into three variables (N, x, n) and a list object

the variables are correctly written, being N = 5, x = 2, n = 100


N, x, n = input().split(' ')
list = [input()]

I've tried using this, but the list only intakes the ['2'], while I need it to be ['2', '8', '1', '3']

I've also tried using while and if loops to try to iterate through the input, but that didn't seem to work for me.

CodePudding user response:

To enter the list you use this approach:

N, x, n = input().split(' ')
lst = []
while True:
    el = input()
    if len(el) > 0:
        lst.append(el)
    else:
        break

Note that you'll have a list of strings, and also N, x and n are strings - so will need to take care of it...

  • Related