Append User Input to List in Python (4 Examples)
In this article, you’ll learn how to append user inputs to a Python list in different ways.
The article will contain this content:
Let’s do this!
I’ll use the following data as a basis for this Python tutorial.
friends_list =  # create empty list for examples 1 and 2 numbers_list =  # create empty list for examples 3 and 4
The next examples will show how to add user inputs to the empty lists created above.
Example 1: Add User Inputs to List One by One
Example 1 illustrates how to add user inputs to a list one at a time using a defined list length.
list_length = 3 # initialize the length of list for idx in range(list_length): # input list item = input('Enter one of your closest friends: ') friends_list.append(item) # Enter one of your closest friends: Yeşim # Enter one of your closest friends: Emir # Enter one of your closest friends: Doğa print("Your 3 best friends: ", friends_list) # print output # Your 3 best friends: ['Yeşim', 'Emir', 'Doğa']
As shown, first, we have initialized the length of the list as
list_length. Then we have set a for loop, which iterates through the range of the defined list length. At each iteration, the user is asked to write the name of one of the closest friends via the input() function. This name is saved as
item then it is appended to
You see that I have input the names of my closest friends: Yeşim, Emir, and Doğa, each time it asked me to enter one of my closest friends. The for loop has terminated after the third name is given. As a result,
friends_list consisted of three strings given by the user. Then I have used the print() function combining the string “Your 3 best friends: ” and
Example 2: Add User Inputs to List at Once
In this example, I’ll explain how to insert multiple user inputs into a list at once. One way to do this is to set a while condition on the minimum number of inputs to be accepted and ask the user to give inputs in at least the given number. If the condition is unmet, the input command is re-executed. In this case, I will favor accepting at least three friend names. See the below code script for the exact implementation.
user_input = '' # define empty string while user_input.count(' ') > 2: # input list user_input = input('Enter at least 3 closest friends space-separated: ') friends_list = user_input.split(' ') # Enter at least 3 closest friends space-separated: Yeşim Doğa Emir print("Your best friends: ", friends_list) # print output # Your best friends: ['Yeşim', 'Emir', 'Doğa']
As seen above, first, I have defined an empty string called
user_input. Then I checked the count of spaces in
user_input via the count() method to ensure that the user enters at least three inputs. Otherwise, the user is asked again to enter at least 3 closest friends space-separated.
When enough inputs are given and pressed enter, the split() method separates the string into a list of substrings based on where the spaces are located. The result is saved in
friends_list and printed with the string “Your best friends: “.
Example 3: Add User Input to List Controlled by Stopping Condition
This example illustrates how to insert user inputs to a list using a stopping condition. We will ask the user to give integer numbers this time. Also, we will let them quit the process via the string input “q”. Since the input() function converts the inputs to strings by default, the user should simply press Q on the keyboard without any quotation marks.
while True: # input list user_input = input("Enter a number or 'q' to quit: ") if user_input == 'q': break integer_input = int(user_input) numbers_list.append(integer_input) # Enter a number or 'q' to quit: 4 # Enter a number or 'q' to quit: 5 # Enter a number or 'q' to quit: 9 # Enter a number or 'q' to quit: q print("The list of numbers you entered is:", numbers_list) # print output # The list of numbers you entered is: [4, 5, 9]
Setting the while condition True creates an infinite loop. The input command takes the user input, and it is saved as
user_input. If the user enters q, the loop terminates by the break statement. If not, the
user_input is converted to an integer via the int() function. Finally,
integer_input is appended to
numbers_list, which has been initialized at the beginning of this tutorial.
Example 4: Add User Input to List Controlled by ValueError
In this example, I’ll show how to warn the user if an inappropriate input format is given. This will be achieved by Pyhton’s try-except block. As long as the user does not quit the process and gives inputs convertible to integers, the user inputs will be appended to the initialized list at each while loop iteration. When the input cannot be converted to an integer, a ValueError will be raised, which will induce an error message.
while True: # input list user_input = input("Enter a number or 'q' to quit: ") if user_input == 'q': break try: integer_input = int(user_input) numbers_list.append(integer_input) except ValueError: print('The provided value is not an integer, please provide an integer value') # Enter a number or 'q' to quit: 4 # Enter a number or 'q' to quit: 5 # Enter a number or 'q' to quit: 3.5 # The provided value is not an integer, please provide an integer value # Enter a number or 'q' to quit: 7 # Enter a number or 'q' to quit: q print("The list of numbers you entered is:", numbers_list) # print output # [4, 5, 7]
In the try block,
user_input is tried to be converted to an integer. If it is doable,
integer_input is added to
numbers_list. If not, a ValueError is raised, which activates the except block printing the message: “The provided value is not an integer, please provide an integer value”. The try block is activated again if the user enters a well-formatted input. The user can always quit the process by pressing q.
As seen, there has been no error message until I input 3.5, which is a floating-point number. Then I enter an input in the right format and quit the process using the input q. The final
numbers_list consisted of the integer numbers 4, 5, and 7.
Video & Further Resources
If you need more explanations on the Python code of this tutorial, I recommend watching the following video on my YouTube channel. In the video, I’m demonstrating the Python programming codes of this tutorial in Python:
The YouTube video will be added soon.
In addition, you might want to look at some of the related posts I have published on my website.
- Append to 2D List in Python
- count() Method for Lists in Python
- Check if List of Lists is Empty in Python
- Find Length of List in Python
- Introduction to Python
In summary: This article has shown how to include user inputs in a list in the Python programming language. Tell me in the comments section below if you have any additional questions. In addition, don’t forget to subscribe to my email newsletter to receive updates on the newest tutorials.
This page was created in collaboration with Cansu Kebabci. Look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview of her other tasks on Statistics Globe.
2 Comments. Leave new
Leave a Reply Cancel reply
Statistics Globe Newsletter
Excellent content and great examples. I love it.
We are happy to hear that you liked the tutorial:) Keep following us! <3