adamsmith
adamsmith

Reputation: 6009

Easiest way to join list of ints in Python?

What is the most convenient way to join a list of ints to form a str without spaces?
For example [1, 2, 3] will turn '1,2,3'. (without spaces).

I know that ','.join([1, 2, 3]) would raise a TypeError.

Upvotes: 3

Views: 3094

Answers (3)

Erik Kaplun
Erik Kaplun

Reputation: 38237

','.join(str(x) for x in ints)

should be the most Pythonic way.

Upvotes: 6

mgilson
mgilson

Reputation: 310069

I like ','.join(str(i) for i in lst), but it's really not much different than map

Upvotes: 3

Dair
Dair

Reputation: 16240

print ','.join(map(str,[1, 2, 3])) #Prints: 1,2,3

Mapping str prevents the TypeError

Upvotes: 5

Related Questions