2 ms·
Joining Strings in Python: A "Huh" Moment
- chrisjj 2y agoMore of a "Meh" moment for me. > Python strings are immutable ... So adding them involves creating new strings all the time, which is inefficient ... Instead, you can append the strings to a list. Lists are mutable, so it’s faster. Then you simply join them all at the end using the str.join() method. Don't we have computers to do such work? Give a man a fish, etc. but give him a smart compiler...
- Doxin 2y agoIIRC this is already optimized these days. Go concat strings naively to your hearts content.
- __s 2y agoPython has a trick allowing mutable concat when reference count is 1 https://github.com/python/cpython/blob/08d09cf5ba041c9c5c3860200b56bab66fd44a23/Objects/unicodeobject.c#L11104 https://github.com/python/cpython/blob/08d09cf5ba041c9c5c386...