1

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.

asked May 9, 2013 at 21:29

2 Answers 2

9

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))
answered May 9, 2013 at 21:29
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, str is the string type in python. So it converts a type into a string: str(5) == '5'
5

Or without map,

bar = ', '.join(str(i) for i in foo)
answered May 9, 2013 at 21:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.