Printing an int list in a single line python3
Solution 1:
Yes that is possible in Python 3, just use *
before the variable like:
print(*list)
This will print the list separated by spaces.
(where *
is the unpacking operator that turns a list into positional arguments, print(*[1,2,3])
is the same as print(1,2,3)
, see also What does the star operator mean, in a function call?)
Solution 2:
You want to say
for i in array:
print(i, end=" ")
The syntax i in array
iterates over each member of the list. So, array[i]
was trying to access array[1]
, array[2]
, and array[3]
, but the last of these is out of bounds (array
has indices 0, 1, and 2).
You can get the same effect with print(" ".join(map(str,array)))
.