[Python-ideas] Add list.join() please
ronmarti18 at gmail.com
Tue Jan 29 00:36:45 EST 2019
If there is a more Pythonic way of joining lists, tuples, sets, etc., it is
by using a keyword and not a method. For example, using a keyword, say
'-' joins ['list', 'of', 'strings']
This is more readable than using the method join() since you can read this
as "dash joins a list of strings". Although, the current method of joining
lists is almost similar to this, the current method is somewhat "confusing"
for beginners or for people who came from other languages.
BTW, this is just what comes in my mind and not supported by Python.
On Tue, Jan 29, 2019 at 1:22 PM Brendan Barnwell <brenbarn at brenbarn.net>
> On 2019-01-28 18:22, David Mertz wrote:
> > On Mon, Jan 28, 2019 at 8:44 PM Jamesie Pic <jpic at yourlabs.org
> > <mailto:jpic at yourlabs.org>> wrote:
> > ['cancel', name].join('_')
> > This is a frequent suggestion. It is also one that makes no sense
> > whatsoever if you think about Python's semantics. What would you expect
> > to happen with this line:
> > ['foo', b'foo', 37, re.compile('foo')].join('_')
> > List are not restricted to containing only strings (or things that are
> > string-like enough that they might play well with joining). Growing a
> > method that pertains only to that specialized sort of list breaks the
> > mental model of Python. Moreover, there is no way to TELL if a
> > particular list is a "list of strings" other than checking each item
> > inside it (unlike in many languages).
> That problem already exists with str.join though. It's just
> spelled this way:
> ','.join(['foo', b'foo', 37, re.compile('foo')])
> . . . and the result is an error. I don't see how it's
> any less sensible to call list.join on a list of non-string things than
> it is to pass a list of non-string things to str.join.
> Personally what I find is perverse is that .join is a method of
> but does NOT call str() on the items to be joined. The cases where I
> would have been surprised or bitten by something accidentally being
> converted to a string are massively outweighed by the cases where I want
> everything to be converted into a string, because, dangit, I'm joining
> them into a bigger string.
> I agree that a list method would be nice, but we then have to
> about should we add similar methods to all iterable types, since
> str.join can take any iterable (not just a list).
> Brendan Barnwell
> "Do not follow where the path may lead. Go, instead, where there is no
> path, and leave a trail."
> --author unknown
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas