Table of contents
1.
Introduction
2.
Get List as Input Using split() Method
2.1.
Example
3.
Using a Loop
3.1.
Example
4.
Using map()
4.1.
Example
5.
Using List Comprehension for Concise Input
5.1.
Example
6.
Accepting a Nested List Input
6.1.
Example
7.
Frequently Asked Questions
7.1.
What happens if I don’t convert input values to integers?
7.2.
Which method is best for taking list input in Python?
7.3.
Can I take list input with comma-separated values instead of spaces?
8.
Conclusion
Last Updated: Aug 22, 2025
Easy

How to Input a List in Python

Author Gaurav Gandhi
0 upvote

Introduction

In Python, you can input a list using the input() function combined with methods like split() or list comprehension. The most common approach is to take user input as a string, split it into elements, and convert them into the desired data type.

How to Input a List in Python

In this article, you will learn different ways to input a list in Python, including handling numbers and strings efficiently.

Get List as Input Using split() Method

The split() method is the easiest way to take space-separated input values and store them in a list.

Example

# Taking input from the user
user_input = input("Enter list elements separated by space: ")

# Using split() method
my_list = user_input.split()
print("The list is:", my_list)
You can also try this code with Online Python Compiler
Run Code

 

Output:

Enter list elements separated by space: 1 2 3 4 5
The list is: ['1', '2', '3', '4', '5']

 

Explanation:

  • The input() function takes input as a string.
     
  • The split() method breaks the string wherever it finds a space and stores the elements in a list.
     
  • The output list contains string elements. You can convert them to integers if needed using map().

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.

Live masterclass