📅  最后修改于: 2023-12-03 14:46:02.985000             🧑  作者: Mango
In Python, we can use a one-liner to print each element of a list using a for loop. This technique is quite useful when we want to quickly iterate over the items of a list and print them.
Here is an example of how to do it:
my_list = [1, 2, 3, 4, 5]
[print(x) for x in my_list]
In the above code, we create a list called my_list
with five elements. We then use a list comprehension with the print()
function inside it to iterate over each element of my_list
and print it.
The output of the above code will be:
1
2
3
4
5
This one-liner is concise and easy to understand, making it a handy tool for quickly printing the elements of a list in Python.
Note: The use of list comprehension here is mainly for its side effect of executing the
print()
function. If you are interested in obtaining a modified list based on the original list, you can use a regular for loop for better readability and maintainability.
I hope you find this information helpful!