I have a list = [1, 2, 3]
I want this to be printed 1, 2, 3
However, when I use:
for number in list:
print(num)
the number gets printed:
1
2
3
I have a list = [1, 2, 3]
I want this to be printed 1, 2, 3
However, when I use:
for number in list:
print(num)
the number gets printed:
1
2
3
For alist = [1, 2, 3]
print(alist)
# [1, 2, 3]
If you don't want the square brackets,
print(', '.join(str(i) for i in alist)
# 1, 2, 3
If you want to print only the elements,
print (*alist)
# 1 2 3
If you insist on using a for
loop,
for i in alist[:-1]:
print(i, end = ", ")
print(alist[-1])
# 1, 2, 3
It sounds like you are asking for the string.join
function
items = [1, 2, 3]
print(', '.join(map(str, items))
which would give
1, 2, 3
You can simply do
list = [1, 2, 3]
print(list)
If you code a "print" inside the "for" loop, the print will be called according to the number of items inside your list, and since each "print" you call, prints in each line, that´s the expected result.
Note to self: I shouldn't use list
because it will shadow the builtin list object.