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

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

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

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


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.

>Solution :

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…

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