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
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.Example:Pythonwith open('file
4 min read