Shifu
Shifu

Reputation: 2185

Converting a list of integers to a string

I have a list in the following manner:

foo=[21, 38, 38, 56, 23, 19, 11, 15, 19, 13, 20, 6, 0, 8, 0, 10, 11, 0, 11, 8, 12, 5]

and I want to convert this into something like:

bar=21, 38, 38, 56, 23, 19, 11, 15, 19, 13, 20, 6, 0, 8, 0, 10, 11, 0, 11, 8, 12, 5

How should this be done? I tried bar=''.join(foo) but this gives me an error message.

Upvotes: 1

Views: 134

Answers (2)

Eric
Eric

Reputation: 97591

Or without map,

bar = ', '.join(str(i) for i in foo)

Upvotes: 5

Dan Lecocq
Dan Lecocq

Reputation: 3483

You're looking for:

''.join(map(str, foo))

This maps each integer through str, which can then be joined together. Though, you may want to add a comma between them:

', '.join(map(str, foo))

Upvotes: 9

Related Questions