In this lesson, You will learn how to get a list as an input in Python. Using the input()
function, you can take a list as input from a user in Python.
Using the Python input() function, we can directly accept strings, numbers, and characters from the user. However, to take a list as input from a user, we need to perform some parsing on user input to convert it into a list. We will see this in the below steps and examples.
Table of contents
How to Get a list of numbers as input from a user in Python
Below are the simple steps to input a list of numbers in Python.
- Use an input() function
Use an input() function to accept the list elements from a user in the format of a string separated by space.
- Use the split() function of the string class
Next, the
split()
method breaks an input string into a list. In Python, thesplit()
method divides a string into multiple substrings based on a specified delimiter. If no delimiter is provided, it defaults to splitting on whitespace. - Use for loop and range() function to iterate a user list
Now, access list elements using a for loop and range() function.
- Convert each element of the list into a number
At the end, convert each element of a list to an integer using an int() function. If you want a list of strings as input, skip this step.
Example: Get a list of numbers as input from a user and calculate the sum of it
input_string = input('Enter elements of a list separated by space \n')
user_list = input_string.split()
print('string list: ', user_list)
# convert each item to int type
for i in range(len(user_list)):
# convert each item to int type
user_list[i] = int(user_list[i])
print('User list: ', user_list)
# Calculating the sum of list elements
print("Sum = ", sum(user_list))
Output:
Enter elements of a list separated by space string list: ['5', '10', '15', '20', '25', '30'] User list: [5, 10, 15, 20, 25, 30] Sum = 105
Note:
The Python input() function always converts the user input into a string and then returns it to the calling program. With those in mind, we converted each element into a number using an int()
function.
If you want to accept a list with float numbers, you can use the float()
function.
Also, Solve:
Input a list using input() and range() function in Python
Let’s see how to accept a list as input without using the split()
method.
- First, create an empty list.
- Next, accept a list size from the user (i.e., the number of elements in a list)
- Next, run the loop till the size of a list using a for loop and range() function
- Now, use the
input()
function to receive a number from a user. - At the end, add the current number to the list using the
append()
function
Example:
number_list = []
n = int(input("Enter the list size "))
print("\n")
for i in range(0, n):
print("Enter number at index", i, )
item = int(input())
number_list.append(item)
print("User list is ", number_list)
Output:
Enter the list size 5 Enter number at index 0 5 Enter number at index 1 10 Enter number at index 2 15 Enter number at index 3 20 Enter number at index 4 25 User list is [5, 10, 15, 20, 25]
Input a list using a list comprehension in Python
List comprehension is a concise way to create lists in Python. It’s a syntactic construct that offers a more readable and often shorter alternative to creating lists using loops.
It is generally a list of iterables generated to include only the items that satisfy a condition.
Let’ see how to use the list Comprehension to get the list as an input from the user.
- First, decide the size of the list.
- Get numbers from the user using the
input()
function. - Split the string on whitespace and convert each number to an integer using a
int()
function. - Append all those numbers to the list.
Example:
n = int(input("Enter the size of the list "))
print("\n")
num_list = list(int(num) for num in input("Enter the list items separated by space ").strip().split())[:n]
print("User list: ", num_list)
Output:
Enter the size of the list 5 Enter the list items separated by space 2 4 6 8 10 User list: [2, 4, 6, 8, 10]
Input a list using the map function
Python’s map() function is a built-in function that applies a given function to all the items in the input list (or any iterable).
Let’s see how to use the map()
function to get a list as input from the user.
- First, decide the list size.
- Next, accept numbers from the user separated by space
- Next, use the
map()
function to wrap each user-entered number in it and convert it into an integer or float as per your need
Example:
n = int(input("Enter the size of list : "))
print("\n")
numList = list(map(float, input("Enter the list numbers separated by space ").strip().split()))[:n]
print("User List: ", numList)
Output:
Enter the size of list : 5 Enter the list numbers separated by space 2.5 5.5 7.5 10.5 12.5 User List: [2.5, 5.5, 7.5, 10.5, 12.5]
Get a list of strings as input from a user
Accepting a list of strings from the user is very straightforward.
- Accept the list of strings from a user in the format of a string separated by space.
- Use the
split()
function on the input string to break a string into a list of words.
Example:
input_string = input("Enter all family members name separated by space ")
# Split string into words
family_list = input_string.split(" ")
print("\n")
# Iterate a list
print("Printing all family member names")
for name in family_list:
print(name)
Output:
Enter all family members name separated by space Jessa Emma Scott Kelly Tom Printing all family member names Jessa Emma Scott Kelly Tom
Accept a nested list as input
In this example, Let’s see how to get evenly sized lists from the user. Let’s see how to accept the following list from a user.
Nested List: [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
Example:
# accept nested list from user
list_size = int(input("Enter the number of sub list "))
print("\n")
final_list = [[int(input("Enter single number and press enter: ")) for _ in range(list_size)] for _ in range(list_size)]
print("List is", final_list)
Output:
Enter the number of sub list 3 Enter single number and press enter: 10 Enter single number and press enter: 20 Enter single number and press enter: 30 Enter single number and press enter: 40 Enter single number and press enter: 50 Enter single number and press enter: 60 Enter single number and press enter: 70 Enter single number and press enter: 80 Enter single number and press enter: 90 List is [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
Next Steps
Let me know your comments and feedback in the section below.
Also, Solve: