How to take List user input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 24, 2022

banner

Photo from Unsplash

Take list user input in Python #

To take list 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. On each iteration, append the input value to the list.
main.py
my_list = [] for _ in range(3): my_list.append(input('Enter a country: ')) print(my_list)

list user input for loop

The code snippet loops 3 times, takes input from the user and appends it 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.

If you need to store integer values in a list, use the int() class to convert the input strings to integers.

main.py
my_list = [] for _ in range(3): try: my_list.append(int(input('Enter a number: '))) except ValueError: print('The provided value is not an integer') print(my_list)

list user input for loop numers

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 words and split the string on the separator.

Take list user input using str.split() #

To take a list 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. The split() method will return a list containing the words the user entered.
main.py
user_input = input('Enter space-separated shopping items: ') shopping_list = user_input.split() print(shopping_list) # 👉️ ['apple', 'banana', 'kiwi']

python list user input split

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.

If you need to convert the values to numbers, use a list comprehension.

main.py
user_input = input('Enter space-separated integers: ').split() list_of_integers = [int(item) for item in user_input] print(list_of_integers) # 👉️ [1, 2, 3]

list user input split 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 list user input using a while loop #

To take list 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. On each iteration, append the input value to the list.
main.py
my_list = [] user_input = '' while len(my_list) < 3: user_input = input('Enter a word: ') my_list.append(user_input) print(my_list)

The example uses a while loop to ask the user for input until the list contains at least 3 items.

You can also use this approach to make sure the list contains at least N integers.

main.py
my_list = [] user_input = '' while len(my_list) < 3: try: user_input = int(input('Enter an integer: ')) my_list.append(user_input) except ValueError: print('Enter a valid integer') continue print(my_list)

list user input at least n values

The example uses a while loop to iterate until the list contains at least 3 integers taken from user input.

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, e.g. in an except block or an if statement.

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

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

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 list of lists user input #

To take a list of lists user input:

  1. Use a for loop to iterate N times.
  2. Use the input() function to take multiple inputs from the user.
  3. 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 = input('Enter a word: ') user_input_2 = input('Enter another word: ') list_of_lists.append([user_input_1, user_input_2]) print(list_of_lists)

user input list of lists

We used a for loop to iterate 2 times.

On each iteration, we take user input 2 times, place the values in a list and append the list to another list.

If you need to take numeric input when using this approach, make sure to use the int() class to convert the input strings to integers.

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.