[Python-ideas] Add list.join() please

Robert Vanden Eynde robertve92 at gmail.com
Tue Jan 29 20:19:07 EST 2019


+1 Good performance analysis IMHO :)

On Tue, 29 Jan 2019, 22:24 Jonathan Fine <jfine2358 at gmail.com wrote:

> I've not been following closely, so please forgive me if I'm repeating
> something already said in this thread.
>
> Summary: str.join allows us to easily avoid, when assembling strings,
> 1. Quadratic running time.
> 2. Redundant trailing comma syntax error.
>
> The inbuilt help(str.join) gives:
>     S.join(iterable) -> str
>         Return a string which is the concatenation of the strings in the
>         iterable.  The separator between elements is S.
>
> This is different from sum in two ways. The first is the separator S.
> The second is performance related. Consider
>     s = 0
>     for i in range(100):
>         s += 1
> and
>     s = ''
>     for i in range(100):
>         s += 'a'
>
> The first has linear running time (in the parameter represented by
> 100). The second has quadratic running time (unless string addition is
> doing something clever, like being lazy in evaluation).
>
> The separator S is important. In Python a redundant trailing comma, like
> so,
>     val = [0, 1, 2, 3,]
> is both allowed and useful. (For example, when the entries are each on
> a simple line, it reduces the noise that arises when an entry is added
> at the end. And when the entries are reordered.)
>
> For some languages, the redundant trailing comma is a syntax error. To
> serialise data for such languages, you can do this:
>     >>> '[{}]'.format(', '.join(map(str, v)))
>     '[0, 1, 2, 3]'
>
> From here, by all means repackage for your own convenience in your own
> library, or use a third party library that already has what you want.
> (A widely used pypi package has, I think, a head start for adoption
> into the standard library.)
>
> By the way, as search for "python strtools" gives me
> https://pypi.org/project/extratools/
> https://www.chuancong.site/extratools/functions/strtools/
>
> https://pypi.org/project/str-tools/. # This seems to be an empty stub.
>
> --
> Jonathan
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190130/125f7dda/attachment.html>


More information about the Python-ideas mailing list