Assuming you are using Python 3:
print(*myList, sep='\n')
This is a kind of unpacking. Details in the Python tutorial: Unpacking Argument Lists
You can get the same behavior on Python 2 using from __future__ import print_function.
With the print statement on Python 2 you will need iteration of some kind. Regarding your question about print(p) for p in myList not working, you can just use the following which does the same thing and is still simple:
for p in myList:
print p
For a solution that uses '\n'.join(), I prefer list comprehensions and generators over map() so I would probably use the following:
print '\n'.join(str(p) for p in myList)
Answer from Andrew Clark on Stack OverflowAssuming you are using Python 3:
print(*myList, sep='\n')
This is a kind of unpacking. Details in the Python tutorial: Unpacking Argument Lists
You can get the same behavior on Python 2 using from __future__ import print_function.
With the print statement on Python 2 you will need iteration of some kind. Regarding your question about print(p) for p in myList not working, you can just use the following which does the same thing and is still simple:
for p in myList:
print p
For a solution that uses '\n'.join(), I prefer list comprehensions and generators over map() so I would probably use the following:
print '\n'.join(str(p) for p in myList)
I use this all the time:
l = [1,2,3,7]
print "".join([str(x) for x in l])
Printing elements in a list, strange differences between list comprehension and standard print
Print a list in another function in Python - Stack Overflow
Lists ( how to print specific item )
Where can I find a comprehensive list of ALL print() arguments?
Hey guys, learning Python and I am just toying around with some simple list comprehension and different ways to tweak lists, and I have this weird interaction I don't understand:
Names_List = ['Fred', 'Velma','Daphne','Shaggy','Scooby','Scrappy'] print(*Names_List, sep = ", ")
This prints each element separated by a comma and space, as I expected.
[print(*i, sep = " & ") for i in Names_List]
This prints every letter of each element in the list separated by a space and & symbol.
Why doesn't it just print each element just like the first example?
EDIT: I see, I wasn't aware of how splatting works and I am iterating through the list and then again through each word in the list.
Thanks reddit!!
"something" is a string, not a list. However ["something"] would be a list of 1 string.
Your code is basically working, just don't forget to call b() somewhere if you want to display the result.
EDIT: Once again, it does work! Except that your're using raw input, so that you expect input from your user.
Run your b function, it will expect some input from your user : just type Florent[return]. your printed list will contain ['Florent']
Most probably, you need to read some good book about generic programming then python before asking questions...
This works
def a():
myList = []
name = myList.append(raw_input("my name"))
b(myList)
def b(lst):
newList=lst
print newList
a()
Change "raw_input" to "input" for python3 and above.