sbb0
sbb0

Reputation: 55

Printing every entry in a list on its own individual line in Python

Say I have an array thusly:

a = [1, 2, 3, 4]

I want to have it be printed like so:

1
2
3
4

but I don't want to go:

print(a[0])
print(a[1])
print(a[2])
print(a[3])

I want it to automatically print each entry, but dynamically enough so that it stops when there is nothing more, but keeps going if there is, if that makes any sense. I'm not very good at words when it comes to programming, but I hope this makes sense.

Upvotes: 0

Views: 2648

Answers (3)

Vincent Savard
Vincent Savard

Reputation: 35907

Obligatory Python3 answer:

[print(k) for k in a]

print is a function in Python3.

Upvotes: 1

Kirk Strauser
Kirk Strauser

Reputation: 30933

Join the list together with linefeeds:

print '\n'.join(a)

Upvotes: 7

Sven Marnach
Sven Marnach

Reputation: 601351

Use a loop:

for x in a:
    print x

Upvotes: 5

Related Questions