aviadch
aviadch

Reputation: 331

Print list without spaces

I have this code here. I want to print a list without spaces. Here l is a list with 3 elements that I am trying to print:

>>> l=[]
>>> l.append(5)
>>> l.append(6)
>>> l.append(7)
>>> print(l)

I get in the output:

[5, 6, 7]

but I want to get:

[5,6,7]

What should I add to the syntax in append or in print to print the list without spaces?

Upvotes: 0

Views: 5946

Answers (4)

user1907906
user1907906

Reputation:

Join the list elements.

print("[" + ",".join([str(i) for i in l]) + "]")

Upvotes: 1

b3orn
b3orn

Reputation: 322

You need to use something like:

print('[{0}]'.format(','.join(map(str, l))))

Upvotes: 5

Emil L
Emil L

Reputation: 21081

You could convert it to a string and then replace the spaces. E.g:

print ("{}".format(l)).replace(' ', '')

Upvotes: 1

viraptor
viraptor

Reputation: 34145

You can modify the result if it isn't too big:

print(repr(l).replace(' ', ''))

Upvotes: 3

Related Questions