In this lesson, You will learn how to input a list in Python.
Using the Python input()
function, we can accept a string, integer, and character input from a user. Now, let see how to get a list as input from a user.
Table of contents
Get a list of numbers as input from a user
How to take a list as input 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 split() function of string class
Next, use a
split()
function to split an input string by space. Thesplit()
method splits a string into a list. - Use for loop and range() function to iterate a user list
Using a
for
loop andrange()
function, we can access each element of the list along with the index number. - Convert each element of list into number
Convert each list element to an integer using a
int()
function.
If you want a list of strings as input then skip this step.
Example 1: 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 ')
print("\n")
user_list = input_string.split()
# print list
print('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])
# Calculating the sum of list elements
print("Sum = ", sum(user_list))
Output:
Enter elements of a list separated by space 5 10 15 20 25 30 list: ['5', '10', '15', '20', '25', '30'] Sum = 105
Note: Python input()
function always converts the user input into a string 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.
Solve:
Input a list using input() and range() function
Let’s see how to accept Python list as an 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)
- Run loop till the size of a list using a
for
loop andrange()
function - use the
input()
function to receive a number from a user - Add the current number to the list using the
append()
function
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
List comprehension is a more straightforward method to create a list from an existing list. 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.
Next, use the list comprehension to do the following tasks
- Get numbers from the user using the input() function.
- Split it string on whitespace and convert each number to an integer using an
int()
function. - Add all that numbers to the list.
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
Let’ see how to use the map() function to get a list as an 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 anint
orfloat
as per your need
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 an input from a user
Accept a string list from the user is very straightforward.
- Accept the list of strings from a user in the format of a string separated by space.
- Use
split()
function on input string to splits a string into a list of words.
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. In simple words, Let’s see how to accept the following list of lists from a user.
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
# 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.
Solve: