how to print a list in python
how to print a list in python
To print a list in Python, you can simply use the print()
function and pass the list as an argument. Here’s an example:
my_list = [1, 2, 3, 4, 5]
print(my_list)
This will output:
[1, 2, 3, 4, 5]
If you want to print each item of the list on a separate line, you can use a for
loop to iterate over the list:
my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item)
This will output:
1
2
3
4
5
You can also use a list comprehension to print each item of the list on a separate line:
my_list = [1, 2, 3, 4, 5]
[print(item) for item in my_list]
This will output:
1
2
3
4
5
Note that this method is not recommended for large lists, as it creates a new list just for the purpose of printing. The for
loop method is more memory-efficient for large lists.