The Wayback Machine - https://web.archive.org/web/20240905170809/https://www.geeksforgeeks.org/python-get-a-list-as-input-from-user/
Open In App

Get a list as input from user in Python

Last Updated : 28 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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
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
Get User IP Address in Django
In web development, knowing a user's IP address can be crucial for a variety of reasons, such as logging, analytics, security, and customization. Django, a powerful web framework for Python, provides several ways to access user IP addresses. In this article, we'll walk through creating a small Django project and demonstrate how to get a user's IP a
2 min read
Get Request.User in Django-Rest-Framework Serializer
In Django Rest Framework (DRF), serializers are used to transform complex data types, such as queryset and model instances, into native Python data types. One common requirement is to access the request.user within a serializer, which is particularly useful when you need to customize the serialization based on the authenticated user. This article w
5 min read
How to Get the Currently Logged In User's ID in Django?
Getting the ID of the currently logged-in user is a very common requirement when building user-centered web applications in Django. Through its built-in authentication system, Django allows easy access to the details of the user who is logged in at any time; this includes their unique ID. We can easily get the user ID when working in views, templat
4 min read
How To Get The Input From A Checkbox In Python Tkinter?
The checkbox is called Checkbutton in Tkinter. Checkbutton allows users to choose more than 1 choice under a category. For example, we need not have only one choice when ordering food from a restaurant. We can choose 2 or 3 or more foods to order from the restaurant. In those applications, we use the check button in Tkinter. How to create a Checkbo
3 min read
How to get keyboard input in PyGame ?
While using pygame module of Python, we sometimes need to use the keyboard input for various operations such as moving a character in a certain direction. To achieve this, we have to see all the events happening. Pygame keeps track of events that occur, which we can see with the events.get() function. In this article, we are going to discuss how we
3 min read
How to Get the Input From Tkinter Text Box?
Tkinter Text box widget is used to insert multi-line text. This widget can be used for messaging, displaying information, and many other tasks. The important task is to get the inserted text for further processing. For this, we have to use the get() method for the textbox widget. Syntax: get(start, [end]) where, start is starting index of required
1 min read
Python | Find all close matches of input string from a list
We are given a list of pattern strings and a single input string. We need to find all possible close good enough matches of input string into list of pattern strings. Examples: Input : patterns = ['ape', 'apple', 'peach', 'puppy'], input = 'appel' Output : ['apple', 'ape'] We can solve this problem in python quickly using in built function difflib.
2 min read
How to get GET request values in Django?
Django, a high-level Python web framework, simplifies the process of web development. One common task in web applications is handling GET requests, which are typically used to retrieve data from a server. In this article, we'll create a small Django project that displays a message using data from a GET request. How to get GET request values in Djan
2 min read
User-defined Exceptions in Python with Examples
Prerequisite: This article is an extension to Exception Handling. In this article, we will try to cover How to Define Custom Exceptions in Python with Examples. Example: class CustomError(Exception): pass raise CustomError("Example of Custom Exceptions in Python") Output: CustomError: Example of Custom Exceptions in Python Python throws errors and
4 min read
Fetch top 10 starred repositories of user on GitHub | Python
Prerequisites: Basic understanding of python, urllib2 and BeautifulSoup We often write python scripts to make our task easier, so here is the script which helps you to fetch top 10 starred repositories of any user on GitHub.You just need Github username (For example: msdeep14) to run the script.Script Explanation: First access the repository url of
4 min read
Python | User groups with Custom permissions in Django
Let's consider a trip booking service, how they work with different plans and packages. There is a list of product which subscriber gets on subscribing to different packages, provided by the company. Generally, the idea they follow is the level-wise distribution of different products. Let's see the different packages available on tour booking servi
4 min read
Python | Fetch your gmail emails from a particular user
If you are ever curious to know how we can fetch Gmail e-mails using Python then this article is for you.As we know Python is a multi-utility language which can be used to do a wide range of tasks. Fetching Gmail emails though is a tedious task but with Python, many things can be done if you are well versed with its usage. Gmail provides IMAP acces
5 min read
Add a User in Linux using Python Script
Creating a user via command line in Linux is a tedious task. Every time someone joins your organization and you need to type a long Linux command rather than doing this you can create a python script that can ask for you the username and password and create that user for you. Examples: Input : Enter Username : John Password: **** Output : User succ
1 min read
Deleting a User in Linux using Python Script
Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel command if you remember the command but if you don't th
2 min read
User Defined Data Structures in Python
In computer science, a data structure is a logical way of organizing data in computer memory so that it can be used effectively. A data structure allows data to be added, removed, stored and maintained in a structured manner. Python supports two types of data structures: Non-primitive data types: Python has list, set, and dictionary as its non-prim
4 min read
Send message to Telegram user using Python
Have you ever wondered how people do automation on Telegram? You may know that Telegram has a big user base and so it is one of the preferred social media to read people. What good thing about Telegram is that it provides a bunch of API's methods, unlike Whatsapp which restricts such things. So in this post, we will be sharing how to send messages
3 min read
Python Tweepy - Getting the screen name of a user
In this article we will see how we can get the screen name of a user. Screen name is the username of the twitter account. It is the name that users choose to identify themselves on the network. Many users choose to either use their real name as the basis for their screen name, often in a shortened version. All the screen names must be unique. Ident
2 min read
Python - User object in Tweepy
Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user.
6 min read
Python Tweepy - Getting the ID of a user
In this article we will see how we can get the ID of a user. ID is the unique identification marker of the twitter account. ID is given to the user by Twitter, the user can not select or change their own ID. In order to get the ID we have to do the following : Identify the user screen name of the profile. Get the User object of the profile using th
2 min read
Python Tweepy - Getting the location of a user
In this article we will see how we can get the location of a user. The location of the user account need not be the exact physical location of the user. As the user is free to change their location, the location of the account can even by a hypothetical place. The location attribute is optional and is Nullable. Identifying the location in the GUI :
2 min read
Python Tweepy - Getting the name of a user
In this article we will see how we can get the name of a user. Name is the display name of the twitter account. It is the name that users choose to identify themselves on the network. Many users choose to either use their real name as the basis for their display name. Unlike screen names, the names need not be unique. Moreover the names can be in a
2 min read
Python Tweepy – Getting the description of a user
In this article we will see how we can get the description of a user. The description of the describes a user account. The description attribute is optional and is Nullable. Identifying the description in the GUI : In the above mentioned profile the description is : Your one-stop destination to attain Coding Nirvana... In order to get the descripti
2 min read
Python Tweepy – Getting the URL of a user
In this article we will see how we can get the URL of a user. The url attribute is a URL provided by the user in association with their profile. The URL attribute is optional and is Nullable. Identifying the URL in the GUI : In the above mentioned profile the url is : geeksforgeeks.org In order to get the URL we have to do the following : Identify
2 min read
Python Tweepy – Getting the number of followers of a user
In this article we will see how we can get the number of followers of a user. The followers_count attribute provides us with an integer donating the number of followers the user account has. Identifying the number of followers in the GUI : In the above mentioned profile the number of followers are : 17.8K (17, 800+) In order to get the number of fo
2 min read
Python Tweepy – Getting the number of lists a user has been added to
In this article we will see how we can get the number of lists a user has been added to. The listed_count attribute provides us with an integer denoting the number of public lists a user has been added to. Private lists are not counted. In order to get the number number of public lists a user has been added to, we have to do the following : Identif
2 min read
Python Tweepy – Getting the time when a user was created
In this article we will see how we can get the time and date when a user created their Twitter account. The created_at attribute provides us with a datetime object denoting the exact time when the user account was created. Identifying the date when a user was created in the GUI : In the above mentioned profile the user was created on July 2009. In
2 min read
Article Tags :
Practice Tags :