How to Put User Input Into a List in Python

In this tutorial, we will discuss how to put user input into a list in Python. The Python programming language provides several ways to do this, but today we will focus on using the .append() method of lists. This will allow us to continually add user-input items to a list on a loop until you decide to break the loop.

Step 1: Start an Infinite Loop

Firstly, start with creating an infinite loop. An infinite loop continues until a specific condition is met. In Python, an infinite loop can be set up in the following way:

This will run continuously until we break it, which we will handle in the next steps.

Step 2: Ask for User Input

Next, prompt the user for input inside the loop. To do this, use the input() function in Python:

Here, we’re asking the user to either enter an item or the letter ‘q’. If user enters ‘q’, we’ll use it to break the loop.

Step 3: Initialize a List and Use append() Method

Initialize an empty list before the loop. Then inside the loop, the .append() method is used to add the user’s input into this list:

The .append() method essentially adds new items at the end of a list.

Step 4: Break the Loop

Finally, apply the logic to break the loop. If the user types ‘q’, the loop will end, else it will keep asking for more items:

Full Code

The console will keep asking for user input until the user types ‘q’. It will then break the loop and print the final list.

Conclusion

Putting user input into a list in Python is a simple process that can be completed in just a few steps. The versatile nature of Python’s list manipulation functions makes it easy to create custom programs based on user input.

Now, you can create programs that keep adding user input to the list until he/she decides to stop it. Continue to experiment with different ways to better processes user input in your Python programs.