In this tutorial, we will learn how to take a list as input in Python in a single line. This can be useful when you want to receive multiple inputs from the user at once or read lines from a data file.
Step 1: Use the input() function
The first step to take a list as input in Python is to use the built-in input()
function. This function waits for the user to type some text followed by the Enter key and then returns the text as a string.
1 |
input_string = input("Enter a list of numbers separated by space: ") |
In this example, we prompt the user to enter a list of numbers separated by spaces. The input is then stored in the variable input_string
.
Step 2: Use the split() method
Since the input()
function returns a string, we need to convert this string into a list by splitting it based on the separator (in this case, a space). We can achieve this using the split()
method.
1 |
input_list = input_string.split(" ") |
Here, we call the split()
method on the input_string
with the space character as the separator. This will return a list of strings, which is now stored in the variable input_list
.
Step 3: Convert string elements to their proper data type
If you need the elements in your list to be of a certain data type (e.g., integers or floats), you can use the map()
function to apply a type conversion function to each element in the input list.
1 |
input_list = list(map(int, input_list)) |
In this example, we use the map()
function to apply the int()
function to each element in the input_list
. After that, we use the list()
function to convert the output of the map()
function back into a list.
Step 4: Combine steps in a single line
You can combine all these steps into a single line of code by nesting the functions.
1 |
input_list = list(map(int, input("Enter a list of integers separated by space: ").split())) |
In this case, we’ve eliminated the need to store intermediate results in separate variables, making the code shorter and more concise.
1 2 3 |
input_string = input("Enter a list of numbers separated by space: ") input_list = input_string.split(" ") input_list = list(map(int, input_list)) |
Here’s the code explained in a single line:
1 |
input_list = list(map(int, input("Enter a list of integers separated by space: ").split())) |
Output:
Enter a list of integers separated by space: 1 2 3 4 5 input_list = [1, 2, 3, 4, 5]
In this tutorial, we’ve learned how to take a list as input in Python in a single line. We’ve used the input()
function to receive user input, the split()
method to separate the string into a list, and the map()
and list()
functions to convert the strings to the desired data type. By combining these functions, we can achieve our goal in just one line of code.
Conclusion
Taking a list as input in a single line can help make your Python code shorter and more efficient. Understanding the input()
, split()
, map()
, and list()
functions, as well as how to combine them effectively, is essential when working with user inputs and data files. Keep practicing to improve your Python skills!