== Exploring the irritating thing about Python's _.join()_ Let's start out with the tweets: > [[@Twirrim > https://twitter.com/Twirrim/status/610312087781441536]]: \\ > ``list_object.join(',')'' > > AttributeError: 'list' object has no attribute 'join' \\ > ``*facepalm*'' > > ','.join(``list_object'') > > [[@thatcks https://twitter.com/thatcks/status/610334538334621697]]: > It's quite irritating that you can't ask lists to join themselves w/ a > string, you have to ask a string to join a list with itself. Python has some warts here and there. Not necessarily big warts, but warts that make you give it a sideways look and wonder what people were thinking. One of them is how you do the common operation of turning a sequence of strings into a single string, with the individual strings separated by some common string like ','. As we see here, a lot of people expect this to be a list operation; you ask the list 'turn yourself into a string with the following separator character'. But that's not how Python does it; instead it's a string operation where you do the odd thing of asking the separator string to assemble a list around itself. This is at least odd and some people find it bizarre. Arguably the logic is completely backwards. There are two reasons Python wound up here. The first is that back in the old days there was no _.join()_ method on strings and this was just implemented as a function in the _string_ module, _string.join()_. This makes perfect sense as a place to put this operation, as it's a string-making operation. But when Python did its great method-ization of various module functions, it of course made most of the _string_ module functions into methods on the _string_ type, so we wound up with the current .join(). Since then it's become Python orthodoxy to invoke list to string joining as '_sep.join(lst)_' instead of '_string.join(lst, sep)_'. The other reason can be illuminated by noting that if Python did it the other way around you wouldn't have just _lst.join()_, you'd also have to have _tuple.join()_ and in fact a _.join()_ method on every sequence compatible type or even iterators. Anything that you wanted to join together into a string this way would have to implement a _.join()_, which would be a lot of types even in the standard library. And because of how both CPython and Python are structured, a lot of this would involve re-implementation and duplication of identical or nearly identical code. If you have to have _.join()_ as a method on *something*, putting it on the few separator types means that you have far less code duplication and that any new sequence type automatically supports doing this in the correct orthodox way. (I'm sure that people would write iterator or sequence types that didn't have a _.join()_ method if it was possible to do so, because sooner or later people leave out every method they don't think they're going to use.) Given the limitations of Python, I'll reluctantly concede that the current _.join()_ approach is the better alternative. I don't think you can even get away with having just _string.join()_ and no string _.join()_ method (however much an irrational bit of me would like to throw the baby out with the bathwater here). Even ignoring people's irritation with having to do '_import string_' just to get access to _string.join()_, there would be some CPython implementation challenges. === Sidebar: The implementation challenges String joining is a sufficiently frequent operation that you want it to be efficient. Doing it efficiently requires doing it in C so that you can do tricks like pre-compute the length of the final string, allocate all of the memory once, and then _memcpy()_ all of the pieces into place. However, you also have both byte strings and Unicode strings, and each needs their own specialized C level string joining implementation (especially as modern Unicode strings have a complex internal storage structure). The existing _string_ module is actually a Python level module. So how do you go from an in-Python _string.join()_ function to specific C code for byte strings or Unicode strings, depending on what you're joining? The best mechanism CPython has for this is actually 'a method on the C level class that the Python code can call', at which point you're back to strings having a _.join()_ method under some name. And once you have the method under some name, you might as well expose it to Python programmers and call it _.join()_, ie you're back to the current situation. I may not entirely like _.join()_ in its current form, but I have to admit that it's an impeccably logically assembled setup where everything is basically the simplest and best choice I can see.