How to read user input into a list in Python?


Python - Read user input into a list

To read the input values entered by user in console input into a list in Python, use input() built-in function and string split() method. input() functions reads the whole user input into a string. And string split() method splits the input string into a list of values.

In this tutorial, you will learn how to read the values entered by user in console input into a list, with examples.

Examples

In the following examples, we consider different scenarios where the separator between the values entered by the user is different. Sometimes the separator could be a comma, sometimes the separator could be a single space, etc.

For example, user can enter the values as space separated values, as shown in the following.

10 20 30 40 50

Or the user can enter the values as input in console, as comma separated values, as shown in the following.

10,20,30,40,50

1. Read space separated values entered by user in console input into a list in Python

In this example, we shall see how to read the space separated values entered by user in console input into a list.

Steps

  1. Prompt user for input using input() built-in function.
  2. Read user input as a string into a variable, say x.
  3. Split the string into values using string split() method with space as separator. By default split() method splits the string by whitespace characters. Therefore, just call the split() method on the input string, no need to specify any separator as argument for the method.
  4. Store the list returned by split() method in a variable, say x_list. You may use the values in the list as per your requirement. In this example, we shall just print the list.

Program

The complete Python program to read the space separated values entered by user in console input into a list is given below.

Python Program

# Read input into a string
x = input("Enter values : ")

# Split input values in string into list
x_list = x.split()

print(f"List : {x_list}")

Output

Enter values : apple banana cherry
List : ['apple', 'banana', 'cherry']

The split() method always returns strings as values in the list. If the values entered by user are numbers, then we may need to transform the values into integers using int() built-in function, as shown in the following program.

Python Program

# Read input into a string
x = input("Enter values : ")

# Split input values in string into list
x_list = x.split()

# Convert the values in list to integers
x_list = [int(x) for x in x_list]

print(f"List : {x_list}")

Output

Enter values : 10 20 30 40 50
List : [10, 20, 30, 40, 50]

2. Read comma separated values entered by user in console input into a list in Python

In this example, we shall see how to read the comma separated values entered by user in console input into a list.

Repeat the same steps in the previous example. The only change in step 3 is to pass comma "," as separator for the string split() method.

Program

The complete Python program to read the comma separated values entered by user in console input into a list is given below.

Python Program

# Read input into a string
x = input("Enter values : ")

# Split input values in string by comma into list
x_list = x.split(",")

print(f"List : {x_list}")

Output

Enter values : apple,banana,cherry
List : ['apple', 'banana', 'cherry']

Summary

In this tutorial, we learned how to read the input values entered by user in console input into a list in Python, going through examples where user enters values with space as separator, or comma as separator.