How to take list input in Python in single line | Example code

  • by

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)

Output:

How to take list input in Python in single line

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)

Output:

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)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.