List As Input in Python in Single Line
Last Updated :
16 Feb, 2024
Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four commonly used methods to take a list as input in Python, making your code more concise and readable.
Example:
Enter elements separated by space:11 12 13 14 15
[11, 12, 13, 14, 15]
How To Take List As Input In Python In Single Line?
Below, are the methods of How To Take a List As Input In Python In a Single Line.
Take List As Input In Python In Single Line Using List Comprehension
In this example, the below code takes a space-separated list of user-input elements, converts them to integers, and stores them in the `user_list`, then prints the resulting list.
Python3
user_list = [int(x) for x in input("Enter elements separated by space:").split()]
print(user_list)
Output
Enter elements separated by space:11 12 13 14 15
[11, 12, 13, 14, 15]
Take List As Input In Python In Single Line Using the map() function
In this example, This code prompts the user to input space-separated elements, converts them to integers using `map()` and `int()`, and then prints the resulting list of integers.
Python3
user_list = list(map(int, input("Enter elements separated by space:").split()))
print(user_list)
Output
Enter elements separated by space:11 12 13 14 15
[11, 12, 13, 14, 15]
Take List As Input In Python In Single Line Using list() & input().split() directly
In this example, below code takes a space-separated list of user-input elements, converts each element to an integer using a generator expression, and stores the integers in the `user_list`, then prints the resulting list.
Python3
user_list = list(int(x) for x in input("Enter elements separated by space:").split())
print(user_list)
Output
Enter elements separated by space:11 12 13 14 15
[11, 12, 13, 14, 15]
Take List As Input In Python In Single Line Using a generator expression
In this example, This code prompts the user to input space-separated elements, converts each element to an integer using a generator expression, and then prints the resulting list of integers.
Python3
user_list = list(int(x) for x in input("Enter elements separated by space:").split())
print(user_list)
Output
Enter elements separated by space:11 12 13 14 15
[11, 12, 13, 14, 15]
Conclusion
In Python, there are multiple ways to take a list as input in just a single line of code. Whether you choose list comprehension, map(), direct list(), or a generator expression depends on your preference and specific use case. By incorporating these techniques into your code, you can enhance its readability and efficiency.
Similar Reads
Get a list as input from user in Python
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 take a list as input from the user using Python.Get list as input Using split() MethodThe input() function can be combined with split() to accept multiple elements in a sin
3 min read
How to Print String and Int in the Same Line in Python
Printing both texts and integers on the same line is a common chore for programmers. Python, fortunately, has various methods for accomplishing this, making it simple to display a combination of text and numerical values in your output. In this article, we'll explore various ways to print strings an
2 min read
How to Initialize a List in Python
Python List is an ordered collections on items, where we can insert, modify and delete the values. Let's first see how to initialize the list in Python with help of different examples. Initialize list using square brackets []Using [] we can initialize an empty list or list with some items. Python# I
2 min read
How to set an input time limit in Python?
In this article, we will explain how to set an input time limit in Python. It is one of the easiest programming languages which is not only dynamically typed but also garbage collected. Here we will look at different methods to set an input time limit. Below are the methods we will use in this artic
6 min read
Convert string to a list in Python
Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Read a file line by line in Python
Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). In this article, we are going to study reading line by line from a file.ExamplePython# Open the file
4 min read
Multiline String in Python
A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double
4 min read
Print Single and Multiple variable in Python
In Python, printing single and multiple variables refers to displaying the values stored in one or more variables using the print() function.Let's look at ways how we can print variables in Python:Printing a Single Variable in PythonThe simplest form of output is displaying the value of a single var
2 min read
Assigning multiple variables in one line in Python
A variable is a segment of memory with a unique name used to hold data that will later be processed. Although each programming language has a different mechanism for declaring variables, the name and the data that will be assigned to each variable are always the same. They are capable of storing val
2 min read
Access List Items in Python
Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from
2 min read