Take N number of inputs in Python

avatar

Borislav Hadzhiev

Last updated: Aug 22, 2022

banner

Photo from Unsplash

Take N number of inputs in Python #

To take N number of inputs:

  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) # 👉️ only do this if taking integer values my_list = [int(item) for item in my_list] print(my_list) # ---------------------------------------- # ✅ using map() my_list = list( map( int, input('Enter space-separated integers: ').split() ) ) print(my_list)

take n number of inputs

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
# 👇️ ['one', 'two', 'three'] print('one two three'.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.

You can use any other separator when taking user input, e.g. a comma.

main.py
my_list = input('Enter comma-separated integers: ').split(',') # 👇️ convert strings to integers (optional) my_list = [int(item) for item in my_list] print(my_list)

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

The str.split() method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

If the separator is not found in the string, a list containing only 1 element is returned.

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]
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 number of inputs:

  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 number 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)

take n number of inputs 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.