We can take a value as input from the user using the input() function. What if we have to get a list of values as input? In this article, we will discuss two ways to get a list as user input in Python.
Get A List As User Input Using The For Loop
We can get a list of values using the for loop in python. For this, we can first create an empty list and a count variable. After that, we will ask the user for the total count of values in the list. After getting the total count of values, we can run the input() function count number of times using the for loop and add the input to the list using the append() method as follows.
input_count = int(input("Input the Total number of elements in the list:"))
input_list = list()
for i in range(input_count):
input_value = input("Enter value {}:".format(i + 1))
input_list.append(input_value)
print("The list given as input by the user is :", input_list)
Output:
Input the Total number of elements in the list:5
Enter value 1:12
Enter value 2:345
Enter value 3:PFB
Enter value 4:12345
Enter value 5:Aditya
The list given as input by the user is : ['12', '345', 'PFB', '12345', 'Aditya']
Get A List As User Input Using The While Loop
Instead of asking the user for total count of input values, we can ask the user to give a special value as input when they want to stop giving values as input. After that, we can keep taking inputs until the user gives the specific value as input signalling that there are no more values left. We can do this using the while loop in python.
In this method, we will first create an empty list and a boolean variable flag. We will initialize the flag to True. Here, flag will work as the decision variable in the while loop. After that we will start taking inputs from users in the while loop and will append them to the list. If the user inputs the specific value signalling that there are no more values left, we will assign False to the flag variable. When the flag’s value turns False, it forces the while loop to terminate.
After the while loop stops execution, we get the list of all the values given as input as seen in the below example.
flag = True
input_list = list()
while flag:
input_value = input("Enter the value in the list. To finish, press enter key without any input:\n")
if input_value == "":
flag = False
continue
input_list.append(input_value)
print("The list given as input by the user is :", input_list)
Output:
Enter the value in the list. To finish, press enter key without any input:
12
Enter the value in the list. To finish, press enter key without any input:
23
Enter the value in the list. To finish, press enter key without any input:
Aditya
Enter the value in the list. To finish, press enter key without any input:
567
Enter the value in the list. To finish, press enter key without any input:
The list given as input by the user is : ['12', '23', 'Aditya', '567']
Get A List As User Input By Using The input() Method Only Once
We know that taking user input is costly in terms of time and resource as the program has to make system calls while taking inputs from the user. So, to maximize the efficiency of the program, we can avoid multiple use of the input() function while taking a list as user input.
For this, we will ask the user to input all the values in the list by separating them using space characters. After taking the space separated values as input, we will use the python string split operation to get a list of all the input values. This can be observed in the following example.
input_values = input("Enter the values in the list separated by space:\n")
input_list = input_values.split()
print("The list given as input by the user is :", input_list)
Output:
Enter the values in the list separated by space:
12 345 Aditya PFB 123345
The list given as input by the user is : ['12', '345', 'Aditya', 'PFB', '123345']
Conclusion
In this article, we have seen different ways to get a list as user input in python. To read about taking inputs from a file in Python, you can read this article on file handling in Python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.