In this post, We are going to learn 4 Ways to take multiple inputs in Python list from the user and append all values in the python list with examples. In programming applications, several times we have need to take some of the inputs from the user. In Python web applications this comes handy when we take the inputs from the user on web forms or web pages. The text or data entered by the user needs to be managed by the application and saved.
In this kind of use case, we need some ways to save this user inputted data into lists. So today we will learn about some of the techniques that we can use to do this job in our applications.
4 Ways to take multiple inputs in list in Python
- The basic way of using a loop.
- Usingc
map()
method. - List of the list as input
- Using List Comprehension
1. take multiple inputs in Python list using loop
The first method is a simple method of using a loop and then appending the input in a list. We can see from the output that we got the desired results.
Python Program to take multiple input
# create an empty list
langlist = []
# enter the number of elements as input
num = int(input("Enter number of elements for list : "))
# running loop till the number of element
for i in range(0, num):
element = input()
langlist.append(element) # appending element one by one
print('list after appending user entered values = \n',langlist)
Output :
Enter number of elements for list : 4
C
c++
C#
Go
list after appending user entered values =
['C', 'c++', 'C#', 'Go']
2. map() method to take multiple inputs in Python list
Second technique which we can use is by using the map() method. The below example demonstrates the technique in detail.
Python Program to take multiple input Using map()
# create an empty list
langlist = []
# enter the number of elements as input
num = int(input("Enter number of elements for list : "))
# read inputs from user by map() function
langlist = list(map(str,input("\nEnter the element : ").strip().split()))[:num]
print('list after appending user entered values = \n',langlist)
Output
Enter number of elements for list : 3
Enter the element : Java C C++
list after appending user entered values =
['Java', 'C', 'C++']
3. take multiple inputs List of list as input
Next technique is by using a list of lists and appending the elements in a list. The below example shows the implementation of this technique. Here is the desired output to confirm the approach is working fine.
Python Program to take list of list input
# create an empty list
langlist = []
# enter the number of elements as input
num = int(input("Enter number of elements for list : "))
for j in range(0, num):
element = [(input()), (input())]
langlist.append(element)
print('list after appending user entered values = \n',langlist)
Output:
Enter number of elements for list : 3
C#
1
Java
2
Python
3
list after appending user entered values =
[['C#', '1'], ['Java', '2'], ['Python ', '3']]
4. List Comprehension to take mutiple input in Python List
The 4th and last technique that we are going to learn is by using list comprehension. The below example is implemented by using this technique.
# list for integers
lstint = []
# list for strings/chars
lststr = []
lstint = [int(element) for element in input("Enter the listInt element : ").split()]
lststr = [element for element in input("Enter the liststr element : ").split()]
print("integer list elements = " ,lstint)
print("string list elements = ",lststr)
Output :
Enter the listInt element : 1 2 3 4 5
Enter the liststr element : C# Java Go
integer list elements = [1, 2, 3, 4, 5]
string list elements = ['C#', 'Java', 'Go']
So in this article, we learned about the techniques to handle the user inputs and save them in lists. This saved data is easy to handle as it is formatted data now and lists are easy to handle.
Summary
In this post, we have learned 4 Ways to take multiple inputs in Python list these techniques are helpful with applications like web pages, Data analysis where we get the data inputs from users or from data sets. We hope you will make use of these techniques in your programs!!