Take N inputs in one line in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Take N inputs in one line in Python #

To take N inputs in one line:

  1. Use the input() function to take multiple, space-separated values.
  2. Use the str.split() function to split the values into a list.
  3. Convert the strings in the list to integers if taking numeric input values.
main.py
# ✅ using list comprehension my_list = input('Enter space-separated integers: ').split() # 👇️ convert strings to integers (optional) my_list = [int(item) for item in my_list] print(my_list) # 👉️ [1, 2, 3, 4, 5] # 👇️ can use list slicing to get first N values print(my_list[:3]) # 👉️ [1, 2, 3] # ---------------------------------------- # ✅ using map() my_list = list( map( int, input('Enter space-separated integers: ').split() ) ) print(my_list)

take n inputs in one line

The input() function expects multiple, space-separated values, e.g. 1 2 3 or a b c.

We used the str.split() function to split a string into a list of multiple values.

main.py
# 👇️ ['a', 'b', 'c'] print('a b c'.split()) # 👇️ ['1', '2', '3'] print('1 2 3'.split())

The str.split() method splits the string into a list of substrings using a delimiter.

When the str.split() method is called without an argument, it considers consecutive whitespace characters as a single separator.

If you only want to take the first N values from a list, use list slicing.

main.py
my_list = [1, 2, 3, 4, 5, 6] print(my_list[:3]) # 👉️ [1, 2, 3] print(my_list[:2]) # 👉️ [1, 2]

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

When only the stop index is provided, the list slice starts at index 0.

If you need to convert the user input values to integers, use a list comprehension.

main.py
my_list = input('Enter space-separated integers: ').split() # 👇️ convert strings to integers (optional) my_list = [int(item) for item in my_list] print(my_list) # 👉️ [1, 2, 3, 4, 5, 6] # 👇️ can use list slicing to get first N values print(my_list[:3]) # 👉️ [1, 2, 3]
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we pass the current value to the int() class to convert it to an integer.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The input() function is guaranteed to return a string, even if the user enters a number.

Alternatively, you can use the map() function.

To take N inputs in one line:

  1. Use the input() function to take multiple, space-separated values.
  2. Use the map() function to convert each input value to an integer if taking numeric inputs.
  3. Use the list() class to convert the map object to a list.
main.py
my_list = list( map( int, input('Enter space-separated integers: ').split() ) ) print(my_list) # 👉️ [1, 2, 3, 4, 5] print(my_list[:3]) # 👉️ [1, 2, 3]

take n inputs in one line map

We used the str.split() function to split the multiple, space-separated integers.

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The map function calls the int() class with each value in the list and converts each string to an integer.

The last step is to use the list() class to convert the map object to a list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.