If I have a list of chars:
a = ['a','b','c','d']
How do I convert it into a single string?
a = 'abcd'
str.join which is the most natural way, a possibility is to use
io.StringIO and abusing
writelines to write all elements in one go:
import io a = ['a','b','c','d'] out = io.StringIO() out.writelines(a) print(out.getvalue())
When using this approach with a generator function or an iterable which isn't a
tuple or a
list, it saves the temporary list creation that
join does to allocate the right size in one go (and a list of 1-character strings is very expensive memory-wise).
If you're low in memory and you have a lazily-evaluated object as input, this approach is the best solution.