Take a list of integers from user Input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 26, 2022

banner

Photo from Unsplash

Take a list of integers from user Input in Python #

To take a list of integers from user input:

  1. Declare a new variable and initialize it to an empty list.
  2. Use the range() class to loop N times in a for loop.
  3. Convert each string to an integer and append the integers to the list.
main.py
list_of_integers = [] for _ in range(3): try: list_of_integers.append(int(input('Enter an integer: '))) except ValueError: print('The provided value is not an integer') print(list_of_integers)

user input list of integers

The for loop iterates 3 times. On each iteration, we take input from the user, convert the value to an integer and append the integer to a list.

The list.append() method adds an item to the end of the list.

I used an underscore for the variable name because we don't need to access it.

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

The function then reads the line from input, converts it to a string and returns the result.

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

We used a try/except statement to handle the ValueError that is raised if the int() class is called with a value that is not a valid integer.

An alternative approach is to ask the user to enter multiple, space or comma-separated integers and split the string on the separator.

Take a list of integers from user Input using str.split() #

To take a list of integers from user input:

  1. Use the input() function to take input from the user.
  2. Use the str.split() method to split the string on each whitespace.
  3. Use a list comprehension to convert each string in the list to an integer.
main.py
user_input = input('Enter space-separated integers: ').split() list_of_integers = [int(item) for item in user_input] print(list_of_integers)

user input list of integers one line

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

When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.
main.py
print('1 5 10'.split()) # 👉️ ['1', '5', '10']

We used a list comprehension to iterate over the list of strings.

main.py
user_input = input('Enter space-separated integers: ').split() list_of_integers = [int(item) for item in user_input] print(list_of_integers)
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 list item to the int() class to convert it to an integer.

Take a list of integers from user Input using a while loop #

To take a list of integers from user input:

  1. Declare a new variable and initialize it to an empty list.
  2. Use a while loop to iterate as long as a condition is met.
  3. Convert each string to an integer and append the integers to the list.
main.py
list_of_integers = [] user_input = '' while len(list_of_integers) < 3: try: user_input = int(input('Enter an integer: ')) list_of_integers.append(user_input) except ValueError: print('Enter a valid integer') continue print(list_of_integers)

user input list of integers while loop

We used a while loop to prompt the user for integer values 3 times.

The continue statement is used to continue to the next iteration of the loop.

If the code in the try block raises a ValueError, the except block runs, where we use the continue statement to continue to the next iteration.

When validating user input in a while loop, we use the continue statement when the input is invalid.

Alternatively, you can use a while True loop with a break statement.

main.py
list_of_integers = [] user_input = '' while True: if len(list_of_integers) >= 3: break try: user_input = int(input('Enter an integer: ')) list_of_integers.append(user_input) except ValueError: print('Enter a valid integer') continue print(list_of_integers)

The if statement checks if the length of the list is equal to or greater than 3.

If the condition is met, we use the break statement to exit out of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

Take a two-dimensional list of integers from user input #

To take a two-dimensional list of integers from user input:

  1. Use a for loop to iterate N times.
  2. Use the input() function to take multiple inputs from the user.
  3. Use the int() class to convert the values to integers.
  4. Add the input values to a list and append the list to the original list.
main.py
list_of_lists = [] user_input = '' for _ in range(2): user_input_1 = int(input('Enter an integer: ')) user_input_2 = int(input('Enter another integer: ')) list_of_lists.append([user_input_1, user_input_2]) print(list_of_lists)

two dimensional list of integers user input

We used a for loop to iterate 2 times.

On each iteration, we take user input 2 times and convert the values to integers.

Lastly, we place the values in a list and append the list to another 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.