How to print elements of a list in a row in Python?


I want to print all the elements in a list, in a horizontal manner.

I did in the following way

a = [1,2,3,4,5]
for i in a:
  print(i)

But the output I'm getting is in vertical format. How can I make it horizontal?

1 Answer

4 years ago by

Python, by default prints every element in a new line.
So, to override that we need to use the end attribute

a = [1,2,3,4,5]
for i in a:
  print(i, end=" ")

Check Output here

4 years ago by Divya