To take list input in Python in a single line use input() function and split() function. Where input() function accepts a string, integer, and character input from a user and split() function to split an input string by space.
Example take list input in Python in a single line
Simple example code.
lst = input('Type separated by space: ').split() # print list print('list: ', lst)
How to read an array of integers from a single line of input in python 3?
Answer: Use map() function along with input and split function. Using int to Read an array of integers only.
The following snippet will map the single line input separated by white space into a list of integers.
lst = list(map(int, input("Type number with space: ").split())) # print list print('list: ', lst)
Type number with space: 1 2 3
list: [1, 2, 3]
Do comment if you have any doubts or suggestions on this Python list input topic
Note: IDE: PyCharm 2021.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.