This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: additional argument for str.join()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, f-brinkmann, steven.daprano
Priority: normal Keywords:

Created on 2021-02-20 20:05 by f-brinkmann, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg387426 - (view) Author: Fabian Brinkmann (f-brinkmann) Date: 2021-02-20 20:05
It would be nice to have an additional argument for str.join() so this

", ".join(["a", "b", "c"], ", and ")

would output

"a, b, and, c"
msg387427 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-02-20 21:37
This seems like a very specific use case. Too specific IMO for a method on all string objects for anyone using Python anywhere in the world. Why not just write a function like this?

    def my_join(strings, sep=", ", last_sep=", and "):
        strings = list(strings)
        return sep.join(strings[:-1]) + last_sep + strings[-1]

    >>> my_join(["one", "two", "three"])
    'one, two, and three'
msg387430 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-02-20 23:40
Python 3.9 is in feature freeze, this couldn't be added before 3.10 now.

You have:

    ", ".join(["a", "b", "c"], ", and ")

outputing this:

"a, b, and, c"

So what are the semantics of this additional argument? To insert it before the last item?
msg387434 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-02-21 01:46
Looking more closely, I think that the semantics are to concatenate the extra argument to the second-last item:

    ", ".join(["a", "b", "c"])
    # -> "a, b, c"

    ", ".join(["a", "b", "c"], ", and")
    # -> "a, b, and, c"

which would be the same as:

    ", ".join(["a", "b, and", "c"])
    # -> "a, b, and, c"


I'm not sure how this is useful. In English, there should never be a comma after the "and", and there possibly shouldn't be a comma after the "b" either, depending on context.

    # Should be: "a, b and c" or "a, b, and c"

See the Oxford or serial comma:

https://thegrammargirls.wordpress.com/tag/oxford-comma/


I'm going to close this feature request. It's too specific and the semantics don't seem to be very useful. But if you would still like to propose this, or a similar change, please discuss it first either on the Python-Ideas mailing list:

    https://mail.python.org/archives/list/python-ideas@python.org/


or at the Ideas topic on

    https://discuss.python.org


so that we can determine the required semantics and get a sense for how useful it would be in general.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87446
2021-02-21 01:46:52steven.dapranosetstatus: open -> closed
resolution: rejected
stage: resolved
2021-02-21 01:46:22steven.dapranosetmessages: + msg387434
2021-02-20 23:40:30steven.dapranosetnosy: + steven.daprano

messages: + msg387430
versions: + Python 3.10, - Python 3.9
2021-02-20 21:37:56Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg387427
2021-02-20 20:06:20f-brinkmannsettitle: addition argument for str.join() -> additional argument for str.join()
2021-02-20 20:05:58f-brinkmanncreate