0

It's never been clear to me why join and split have the opposite syntax from each other in Python. As in the below example:

some_list = ['a', 'b', 'c']
some_str = ' '.join(some_list)

some_str = 'a.b.c.'
some_list = some_str.split('.')

Why isn't it some_list.join(' '), or conversely, '.'.split(some_str)? Is there a historical reason for these two syntaxes being different, or is it because of something you can do with one syntax that you can't with the other?

Lou
  • 2,200
  • 2
  • 33
  • 66
  • The `''.join` syntax is great because you can use it on all kinds of iterators and even generator comprehensions, not just lists which would be inefficient – mousetail Sep 01 '22 at 08:59
  • @mkrieger1 - Granted, but the order of operands is (to me) flipped. For `join`, why do you call a method on the joining character, not on the list? – Lou Sep 01 '22 at 09:01
  • Because *you can use it on all kinds of iterators and even generator comprehensions.* – deceze Sep 01 '22 at 09:03
  • Okay, the dupe does answer my question, so that's fine, thanks. I'm curious why comments keep disappearing - do authors commonly delete their own comments or do mods periodically clean up certain comments? – Lou Sep 01 '22 at 09:04
  • @deceze - That point didn't answer my question about the order of operands though – Lou Sep 01 '22 at 09:04
  • 1
    Well, it's not *operands*, it's that `join` is a method of `str`, so that it can accept *all kinds of iterators and even generator comprehensions*. The other way around `join` would need to be a method of *all kinds of iterators and even generator comprehensions* instead. – deceze Sep 01 '22 at 09:06
  • Ahh, now I'm with you. Thanks for explaining :) – Lou Sep 01 '22 at 09:07

0 Answers0