Open In App

How to input multiple values from user in one line in Python?

Last Updated : 20 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The goal here is to take multiple inputs from the user in a single line and process them efficiently, such as converting them into a list of integers or strings. For example, if the user enters 10 20 30 40, we want to store this as a list like [10, 20, 30, 40]. Let’s explore different approaches to achieve this in Python.

Using map()

map() method is concise and highly efficient to reads the entire input in one line, splits it by spaces and maps each value to the desired type (e.g., int).

Python
a = list(map(int, input().split()))
print(a)

Output

Output

Using map()

Explanation: When the user enters space-separated values (e.g., “10 20 30”), input() reads the line as a string. split() breaks it into a list of strings like [“10”, “20”, “30”]. map(int, …) converts each string to an integer and list() turns the result into a list of integers.

Using list comprehension

This is almost as efficient as map(), but more Pythonic for those who prefer list comprehensions. It also reads input in one line.

Python
a = [int(x) for x in input().split()]
print(a)

Output

Output

Using list comprehenison

Explanation: input() reads a line of space-separated values as a string. split() breaks it into a list of strings. The list comprehension [int(x) for x in …] converts each string to an integer.

Using split() and for loop

In this approach, the input is split manually and then each value is converted inside a loop. It’s more verbose and less performant.

Python
a = input().split()

for i in range(len(a)):
    a[i] = int(a[i])
print(a)

Output

Output

Using split() and for loop

Explanation: input().split() reads space-separated values and stores them as strings in a list a. The for loop converts each string to an integer and updates the list in place.

Using input() in a for loop

This method asks for each value separately using a loop. It is very inefficient for large inputs and should generally be avoided unless you want to handle inputs one by one.

Python
a = []
n = int(input("Enter number of values: "))

for i in range(n):
    a.append(int(input(f"Enter value {i+1}: ")))
print(a)

Output

Output

Using input() in a for loop

Explanation: This code takes an integer n as the number of inputs, then uses a loop to read and convert each input to an integer, appending them to list a.


Next Article
Article Tags :
Practice Tags :

Similar Reads