How to take Array user input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 26, 2022

banner

Photo from Unsplash

Take array user input in Python #

To take an array 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
arr = [] for _ in range(3): arr.append(input('Enter a word: ')) print(arr)

user input array

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
arr = [] for _ in range(3): try: arr.append(int(input('Enter an integer: '))) except ValueError: print('The provided value is not an integer') print(arr)

user input array of integers

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 array user input using str.split() #

To take an array 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 words: ') arr = user_input.split() print(arr)

array user input 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.

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

main.py
user_input = input('Enter space-separated integers: ').split() arr_of_integers = [int(item) for item in user_input] print(arr_of_integers)

input array of integers one line

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

To take array 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
arr = [] user_input = '' while len(arr) < 3: user_input = input('Enter a word: ') arr.append(user_input) print(arr)

array user input while loop

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
arr = [] user_input = '' while len(arr) < 3: try: user_input = int(input('Enter an integer: ')) arr.append(user_input) except ValueError: print('Enter a valid integer') continue print(arr)

array user input while loop integers

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.

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

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

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.

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.