-1

is there a way to print a list for its element in a single line without using a loop ?

a = [1, 2, 3]

output - 1 2 3

the only condition is not to use a loop. So basically maybe taking list as an object and convert it to another object like the output and print. Just dont want to use refer the list element by element like in a for or any other loop.

  • 1
    Does this answer your question? [Printing an int list in a single line python3](https://stackoverflow.com/questions/37625208/printing-an-int-list-in-a-single-line-python3) – buran Sep 23 '20 at 06:41

3 Answers3

0

As seen in that post from buran, you can use this line:

print(*a)
Alice F
  • 435
  • 5
  • 13
0

A way using indexes simply is:

print(a[:])
Yuri
  • 27
  • 6
0

As a comparison of both:

>>> a = [1, 2, 3]
>>> print(a)
[1, 2, 3]
>>> print(a[:])
[1, 2, 3]
>>> print(*a)
1 2 3

Therefore, *a is the answer you are looking for.

Note: * before a variable does an "unzip" or "depackage", meaning it will return elements of a tuple/list/string as 1 by 1.

Hope this was useful ;)

MrJavy
  • 196
  • 6