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
Shifu
2,1953 gold badges17 silver badges15 bronze badges
2 Answers 2
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
Dan Lecocq
3,5231 gold badge28 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Dan Lecocq
Ah,
str is the string type in python. So it converts a type into a string: str(5) == '5'Or without map,
bar = ', '.join(str(i) for i in foo)
answered May 9, 2013 at 21:34
Eric
98.1k54 gold badges257 silver badges389 bronze badges
Comments
lang-py