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

Jonathan Fine jfine2358 at gmail.com
Tue Jan 29 16:21:48 EST 2019

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
    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/str-tools/. # This seems to be an empty stub.


More information about the Python-ideas mailing list