How to Input a List Using Loops in Python

TL;DR
Python's input() method always returns user input as a string, so typing a list of numbers gives you one string of characters, not separate items. To store values as individual list items, ask the user how many elements they want, create an empty list, then use a for loop with int(input()) and append() to add each typed value.
Transcript
foreign in the last presentation we have understood how to use input method to receive input from the user and we have learned how to receive simple data from the user now in this presentation we will learn how to input a list using Loops so without any further delay let's get started the first topic of this presentation is problem with the input m... Read More
Key Insights
- The input() method always returns user input as a string, never an integer or any other data type, which becomes a problem when you want to store multiple values as separate list items.
- Typing a list like '67 48 90' directly into input() produces a single string of eight characters (each digit and space is one item), not a three-item list of numbers 67, 48, and 90.
- To build a list from user input, first ask how many elements the user wants using n = int(input('enter the number of elements')), type-casting the result to an integer.
- Type-casting with int() around input() is required so n points to the integer 3 rather than the character '3', letting it be used correctly by the loop.
- An empty list is created with numbers = [] before the loop, giving a container to append each incoming value to.
- A for loop with the syntax 'for i in range(n):' repeats the enclosed code n times, with i taking values from 0 to n minus 1, and requires a colon and indentation.
- Inside the loop, x = int(input()) receives and type-casts each value, then numbers.append(x) adds that value to the end of the list.
- The append() method adds items at the end of the list, so calling it once per loop iteration builds up the list to the exact three items 67, 48, and 90 the user intended.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: Why does the input() method return numbers as a string in Python?
The input() method in Python always returns user input as a string, never as an integer or any other data type. This is its default behavior regardless of what the user types. So if you ask the user to enter a list of numbers and they type '67 48 90', Python treats the entire entry as one string. This becomes a problem when you want to store the values as individual list items, because the whole input is captured as a single string rather than separate numbers.
Q: What happens if you type a list of numbers directly into input()?
If you type something like '67 48 90' directly into input(), you do not get a three-item list of numbers. Instead the variable points to a single string, and if treated as a list it contains eight items where every character is a separate element. That includes the character six, character seven, a white-space character, character four, character eight, and so on. You expected three items with values 67, 48, and 90 at indices 0, 1, and 2, but you get eight individual characters instead.
Q: How do you input a list of numbers using loops in Python?
First ask the user how many elements they want with n = int(input('enter the number of elements')), type-casting the input to an integer. Then create an empty list with numbers = []. Next run a for loop using 'for i in range(n):' which repeats n times. Inside the loop, receive each value with x = int(input()) and add it to the list with numbers.append(x). After the loop finishes, the numbers list contains each value the user entered as a separate item, such as 67, 48, and 90.
Q: Why do you need to use int() with the input() method?
You need int() around input() because input() always returns a string, and you often want the value stored as an integer instead. Wrapping it as int(input(...)) type-casts the received input into an integer. For example, when asking for the number of elements, int(input('enter the number of elements')) makes n point to the integer 3 rather than the character '3'. This type-casting is done at the moment of input so the value can be used correctly in operations like range() and in building a numeric list.
Q: What does the append() method do in Python?
The append() method adds an item at the end of a list. In this example, numbers.append(x) takes the value that x is pointing to and places it at the end of the numbers list. If the list starts empty and the first input is 67, calling numbers.append(x) makes the list contain one item, 67. Appending the next value, 48, gives a two-item list, and so on. Running append() once per loop iteration builds the list up to its full set of entered values.
Q: What does range(n) do in a for loop?
In the syntax 'for i in range(n):', range(n) means the values go from 0 to n minus 1. The loop variable i receives these values one by one, so the code inside the loop runs n times. If n is 3, the loop runs three times with i taking values 0, 1, and 2. This lets you repeat a piece of code a set number of times, which is exactly what you need to receive and store a known number of inputs into a list.
Q: Why do you need to type-cast input when it is already received from the user?
Type-casting is needed because input() always hands back a string, so without conversion your value would be a character, not a number. When you type 3 for the number of elements, int(input(...)) ensures n points to integer 3 and not character 3. The video confirms this matters: because the input was already type-cast, n points to integer 3. The same applies inside the loop, where int(input()) ensures each stored value is a number that can be used in numeric contexts.
Q: How can you check that all inputs were correctly stored in the list?
After the loop finishes, you type the list variable name, numbers, and hit enter in the Python interactive shell. If the inputs were received and stored correctly, it displays the list with the expected items, for example a three-item list containing 67, 48, and 90. Getting back the prompt without an error after entering the values also indicates the inputs were received successfully and stored in the numbers variable; an error would appear instead if something went wrong.
Summary & Key Takeaways
-
The input() method can receive user input but always returns it as a string. If you ask a user to enter a list of numbers and they type '67 48 90', the numbers variable points to a single string, not a usable list of number values.
-
Visualizing the result shows the difference: instead of a three-item list with values 67, 48, and 90 at indices 0, 1, and 2, you get an eight-item list where each character (including white spaces) is a separate item. This is not what you want.
-
The solution uses loops. Ask the user how many elements with n = int(input(...)), create an empty list numbers = [], then run 'for i in range(n):' to loop n times, using x = int(input()) and numbers.append(x) each iteration to store the three values 67, 48, and 90 correctly.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from Neso Academy 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator





