How can I print a list, say [‘White’,’Yellow’,’Blue’,’Red’,’Orange’]
like a “list of words?” That means that I want my list to be printed like so:
White
Yellow
Blue
Red
Orange
How can I print a list, say [‘White’,’Yellow’,’Blue’,’Red’,’Orange’]
like a “list of words?” That means that I want my list to be printed like so:
White
Yellow
Blue
Red
Orange
What about using a loop?
>>> colors = ['White', 'Yellow', 'Blue', 'Red', 'Orange']
>>> for color in colors:
... print(color)
...
White
Yellow
Blue
Red
Orange
Or create a string separated by line feeds and print it directly
>>> print('\n'.join(colors))
White
Yellow
Blue
Red
Orange
You could also use join
:
colors = ['White', 'Yellow', 'Blue', 'Red', 'Orange']
print('\n'.join(colors))
At least three obvious ways I can think of off-hand:
Straightforward print loop:
for x in iterable:
print(x)
print
with varargs unpacking and a newline separator:
print(*iterable, sep="\n")
Explict join with newline:
print("\n".join(iterable))
From here on out it gets more esoteric, but one particular case worth mentioning (because it's basically a faster version of the explicit loop that avoids trying to pull a complete iterator into memory at once, and avoids per-item bytecode execution on the CPython reference interpreter by pushing all the work to C):
import sys
sys.stdout.writelines(map('{}\n'.format, iterable))
You can just iterate on your list and print each word.
Just like this:
list = ["White","Yellow","Blue","Red","Orange"]
for word in list:
print(word)