Open In App

Get a list as input from user in Python

Last Updated : 28 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to get input a list from the user using Python

Example: 

Input : n = 4,  ele = 1 2 3 4
Output : [1, 2, 3, 4]

Input : n = 6, ele = 3 4 1 7 9 6
Output : [3, 4, 1, 7, 9, 6]

Get a list as input from user in Python using Loop

Python3




# creating an empty list
lst = []
 
# number of elements as input
n = int(input("Enter number of elements : "))
 
# iterating till the range
for i in range(0, n):
    ele = int(input())
    # adding the element
    lst.append(ele) 
 
print(lst)


Output: 

Python | Get a list as input from user

 

Time Complexity: O(n), where n is the length of the list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

Get a list as input from user in Python With exception handling  

Python3




# try block to handle the exception
try:
    my_list = []
 
    while True:
        my_list.append(int(input()))
 
# if the input is not-integer, just print the list
except:
    print(my_list)


Output: 

Python | Get a list as input from user

 

Get a list as input from user in Python Using map() 

Python3




# number of elements
n = int(input("Enter number of elements : "))
 
# Below line read inputs from user using map() function
a = list(map(int,
    input("\nEnter the numbers : ").strip().split()))[:n]
 
print("\nList is - ", a)


Output: 

Python | Get a list as input from user

 

Get a list as input from user in Python List of lists as input 

Python3




lst = []
n = int(input("Enter number of elements : "))
 
for i in range(0, n):
    ele = [input(), int(input())]
    lst.append(ele)
 
print(lst)


Output: 

Python | Get a list as input from user

 

Get a list as input from user in Python Using List Comprehension and Typecasting 

Python3




# For list of integers
lst1 = []
 
# For list of strings/chars
lst2 = []
 
lst1 = [int(item) for item in input("Enter \
                the list items : ").split()]
 
lst2 = [item for item in input("Enter \
                the list items : ").split()]
 
print(lst1)
print(lst2)


Output: 

Python | Get a list as input from user



Previous Article
Next Article

Similar Reads

Get User Input in Loop using Python
In Python, for and while loops are used to iterate over a sequence of elements or to execute a block of code repeatedly. When it comes to user input, these loops can be used to prompt the user for input and process the input based on certain conditions. In this article, we will explore how to use for and while loops for user input in Python. User I
3 min read
Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then the order of matrix is given by r x c. Each entrie
3 min read
Python program to create dynamically named variables from user input
Given a string input, our task is to write a Python program to create a variable from that input (as a variable name) and assign it to some value. Below are the methods to create dynamically named variables from user input. Using globals() method to create dynamically named variables Here we are using the globals() method for creating a dynamically
2 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Step 1: First, we will take the data from the user and
2 min read
Save user input to a Excel File in Python
In this article, we will learn how to store user input in an excel sheet using Python, What is Excel? Excel is a spreadsheet in a computer application that is designed to add, display, analyze, organize, and manipulate data arranged in rows and columns. It is the most popular application for accounting, analytics, data presentation, etc. Methods us
4 min read
How to input multiple values from user in one line in Python?
For instance, in C we can do something like this: C/C++ Code // Reads two values in one line scanf("%d %d", &x, &y) One solution is to use raw_input() two times. C/C++ Code x, y = input(), input() Another solution is to use split() C/C++ Code x, y = input().split() Note that we don't have to explicitly specify spli
2 min read
How To Add User Input To A Dictionary In Python
In Python, a dictionary is a built-in data type that represents an unordered collection of key-value pairs. Dictionaries are sometimes referred to as "dicts." They provide a way to store and retrieve data efficiently based on keys. Dictionaries in Python are defined using curly braces {}. In this article, we will add user input to a dictionary in P
3 min read
User Input in PySimpleGUI
It is important how keys are key for understanding PySimpleGUI elements. If the user does not specify a key, then the element will be called an input element, a key will be provided to the user by default in integer form, starting the numbering with zero. If the user doesn't specify any keys, then the values returned will be returned as a list as t
1 min read
PyQt5 - Create a User form to get information
In this article we will see how we can create a user form in PyQt5. A user form is basically a dialog box that makes a user data entry more controllable and easier to use for the user. Sometimes while creating a large user interface application there is need of creating user form in order to get essential information. Implementation steps : 1. Crea
3 min read
Python program to get the indices of each element of one list in another list
Given 2 lists, get all the indices of all occurrence of each element in list2 from list1. Input : test_list = [4, 5, 3, 7, 8, 3, 2, 4, 3, 5, 8, 3], get_list = [7, 5, 4] Output : [[3], [1, 9], [0, 7]] Explanation : 5 is present at 1st and 9th index. Input : test_list = [4, 5, 3, 7, 8, 3, 2, 4, 3, 5, 8, 3], get_list = [7, 5, 8] Output : [[3], [1, 9],
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg