Getting list as user_input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Getting list as user_input

I started learning python a few months ago. Recently, I am trying to write a program that will enable me solve Correlation coefficient on the go but am stuck somehow in getting the user values of X and Y respectively. Is there anyway I can make user input(integer x and y) to be in the form of a list either by asking for an arrayof int separated by commas? Please fellow python programmer, I need your contribution

19th Feb 2017, 3:40 PM
Olasubomi Oyetoso
Olasubomi Oyetoso - avatar
2 Answers
+ 3
You can get it along the routes of having the user enter multiple values on the same line but the entries will be separated with spaces. Then you can have a list comprehension that'll be in the form of a one-liner for loop that'll iterate over the list and convert each element from a string to an integer... aList = input('Enter values separated by space:').split() aList = [int(i) for i in aList] print(aList)
19th Feb 2017, 4:54 PM
Don
Don - avatar
+ 2
This can be separated by what you want, you just have to pass it to the split() method: aList = input("Enter values separated by comma: ').split(",")
19th Feb 2017, 9:03 PM
visph
visph - avatar