-2

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

4 Answers4

1

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
Querenker
  • 2,242
  • 1
  • 18
  • 29
1

You could also use join:

colors = ['White', 'Yellow', 'Blue', 'Red', 'Orange']

print('\n'.join(colors))
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
1

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))
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

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)
Jacobb
  • 41
  • 5