I got the results I wanted by doing this:
list = [1, 2, 3, 4, 5]
def new(x):
for i in x:
print i
and then calling new(list)
Results would be:
1
2
3
4
5
What I would like to do is something like print [x for x in list]
but I can't seem to find the right answer searchin. Is there a more compact way to plint out each item in the list on new lines?
On Python 3.x , simply add a sep
while you print
:
lst = [1, 2, 3, 4, 5]
print(*lst, sep="\n")
# 1
# 2
# 3
# 4
# 5
EDIT : On Python 2.6+ , you need to import print_function
:
from __future__ import print_function
lst = [1, 2, 3, 4, 5]
print(*lst, sep='\n')
I think what you are looking for is :
new = lambda x:"\n".join(map(str,x))
print new(x)
Here an answer without lambda
and map()
:
ls = [1, 2, 3, 4, 5]
print('\n'.join([str(a) for a in ls]))
Also, please do not call a variable list
as this interferes with the class
list
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.