Using a Loop
If the user needs to enter elements one by one, a loop can be used.
Example
# Taking number of elements as input
n = int(input("Enter the number of elements: "))
my_list = []
for i in range(n):
element = input(f"Enter element {i+1}: ")
my_list.append(element)
print("The list is:", my_list)

You can also try this code with Online Python Compiler
Run Code
Output
Enter the number of elements: 3
Enter element 1: apple
Enter element 2: banana
Enter element 3: cherry
The list is: ['apple', 'banana', 'cherry']
Explanation:
- The program first asks for the number of elements.
- A loop is used to take individual inputs and store them in a list.
Using map()
The map() function allows easy conversion of input elements to integers, floats, or any other type.
Example
# Taking input from the user
user_input = input("Enter numbers separated by space: ")
# Using map to convert strings to integers
my_list = list(map(int, user_input.split()))
print("The list is:", my_list)

You can also try this code with Online Python Compiler
Run Code
Output:
Enter numbers separated by space: 10 20 30 40 50
The list is: [10, 20, 30, 40, 50]
Explanation:
- The split() method breaks the input string into a list.
- The map() function converts each string element to an integer.
- The list() function stores the mapped elements as a list.
Using List Comprehension for Concise Input
List comprehension is a compact way to take and process list input.
Example
# Taking input from the user
my_list = [int(x) for x in input("Enter numbers separated by space: ").split()]
print("The list is:", my_list)

You can also try this code with Online Python Compiler
Run Code
Output:
Enter numbers separated by space: 5 15 25 35
The list is: [5, 15, 25, 35]
Explanation:
- The expression [int(x) for x in input().split()] creates a list using comprehension.
- Each element is converted to an integer.
- This method is shorter and more efficient.
Accepting a Nested List Input
To take nested lists as input, a loop or list comprehension can be used.
Example
# Taking number of rows as input
rows = int(input("Enter number of rows: "))
nested_list = []
for i in range(rows):
row = list(map(int, input(f"Enter space-separated numbers for row {i+1}: ").split()))
nested_list.append(row)
print("The nested list is:", nested_list)

You can also try this code with Online Python Compiler
Run Code
Output:
Enter number of rows: 2
Enter space-separated numbers for row 1: 1 2 3
Enter space-separated numbers for row 2: 4 5 6
The nested list is: [[1, 2, 3], [4, 5, 6]]
Explanation:
- The program first takes the number of rows.
- Each row is entered separately and converted into a list.
- The lists are stored inside another list, forming a nested list.
Frequently Asked Questions
What happens if I don’t convert input values to integers?
By default, input() takes values as strings. Without conversion, numerical operations will not work correctly.
Which method is best for taking list input in Python?
The split() method is simplest for basic input. map() is useful when conversion is needed. List comprehension is best for concise code.
Can I take list input with comma-separated values instead of spaces?
Yes, you can take comma-separated input by using the split(",") method, which will separate the values based on commas instead of spaces.
Conclusion
In this article, we discussed multiple ways to take list input in Python. We covered methods like split(), loops, map(), list comprehension, and nested lists. Each method has its advantages depending on the use case. By understanding these techniques, you can efficiently handle user inputs in your Python programs.