The join()
function accepts an iterable as parameter. However, I was wondering why having:
text = 'asdfqwer'
This:
''.join([c for c in text])
Is significantly faster than:
''.join(c for c in text)
The same occurs with long strings (i.e. text * 10000000
).
Watching the memory footprint of both executions with long strings, I think they both create one and only one list of chars in memory, and then join them into a string. So I am guessing perhaps the difference is only between how join()
creates this list out of the generator and how the Python interpreter does the same thing when it sees [c for c in text]
. But, again, I am just guessing, so I would like somebody to confirm/deny my guesses.