[issue40344] Programming FAQ about "What is the most efficient way to concatenate many strings together?" -- Improving the example

New submission from Dominik V. <dominik.vilsmeier1123@gmail.com>: The section mentions the usage of `str.join` and contains the following example: chunks = [] for s in my_strings: chunks.append(s) result = ''.join(chunks) Since `join` accepts any iterable the creation of the `chunks` list in a for loop is superfluous. If people just copy & paste from this FAQ they'll even end up with less performant code. The example could be improved by providing an example list such as: strings = ['spam', 'ham', 'eggs'] meal = ', '.join(strings) Arguably this isn't a particularly long list of strings, so one more example could be added using e.g. `range(100)`: numbers = ','.join(str(x) for x in range(100)) This also emphasizes the fact that `join` takes any iterable rather than just lists. ---------- assignee: docs@python components: Documentation messages: 366887 nosy: Dominik V., docs@python priority: normal severity: normal status: open title: Programming FAQ about "What is the most efficient way to concatenate many strings together?" -- Improving the example type: enhancement versions: Python 3.8, Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Dominik V. <dominik.vilsmeier1123@gmail.com> added the comment: Here's the link to the relevant section: https://docs.python.org/3/faq/programming.html#what-is-the-most-efficient-wa... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: Dominik, can you please limit your tracker issues to a handful on entries that you really care about? This is turning into a stream of consciousness dump onto our tracker. You really don't need to rewrite every entry you see, especially when we haven't had user complaints about the existing entries. Also, it seems to me that you're missing the point of the simplified examples in the docs. Yes, of course, the for-loop in chunks is superfluous; however, that for-loop is very common pattern, but it typically does other work in the loop. For example: blocks = [] while True: block = s.recv(4096) if not block: break blocks.append(block) page = b''.join(blocks) The problem with that example is that it shifts focus to tcp clients rather than the core topic to how to join strings. ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Dominik V. <dominik.vilsmeier1123@gmail.com> added the comment: It was not my intention to disturb the traffic on the bug tracker. My apologies if that caused any trouble. I also thought only people subscribed to the indicated topic (e.g. "Documentation") would receive a notification. The docs pages mention that for enhancement proposals one should submit a bug report on the tracker:
If you find a bug in this documentation or would like to propose an improvement, please submit a bug report on the tracker (https://docs.python.org/3/bugs.html).
I do care about the quality of Python's documentation and I think it could be improved in these cases. Often it is newcomers who consult these pages and they might be irritated by the mentioned parts. I see how it would be distracting to include a more complex real world example, but using an example which performs apparently superfluous steps without any additional comment might seem strange. More experienced users probably won't need such an example at all. In addition it might make people falsely belief that `str.join` expects a list of strings rather than any iterable, and hence the explicit construction of the list. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Andy Lester <andy@petdance.com>: ---------- nosy: +petdance _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: Your contributions are welcome. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <batuhanosmantaskaya@gmail.com>: ---------- keywords: +patch nosy: +BTaskaya nosy_count: 4.0 -> 5.0 pull_requests: +19103 stage: -> patch review pull_request: https://github.com/python/cpython/pull/19782 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <batuhanosmantaskaya@gmail.com>: ---------- pull_requests: -19103 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Batuhan Taskaya <batuhanosmantaskaya@gmail.com> added the comment: Sorry for the noise, wrong issue (thought 344, but actually was 334) ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <batuhanosmantaskaya@gmail.com>: ---------- pull_requests: +19105 pull_request: https://github.com/python/cpython/pull/19782 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <batuhanosmantaskaya@gmail.com>: ---------- pull_requests: -19105 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <batuhanosmantaskaya@gmail.com>: ---------- keywords: -patch _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <batuhanosmantaskaya@gmail.com>: ---------- stage: patch review -> _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <isidentical@gmail.com>: ---------- keywords: +patch pull_requests: +19624 stage: -> patch review pull_request: https://github.com/python/cpython/pull/20360 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <isidentical@gmail.com>: ---------- pull_requests: -19624 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <isidentical@gmail.com>: ---------- pull_requests: +19625 pull_request: https://github.com/python/cpython/pull/20360 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Change by Batuhan Taskaya <isidentical@gmail.com>: ---------- pull_requests: -19625 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________

Andy Lester <andy@petdance.com> added the comment: I'd also like to suggest that the question not be "most efficient" but "fastest". I don't think it should treat "efficient" and "fast" as synonyms. "Efficient" can mean things other than execution speed, such as memory usage, or programmer time. Are there space/time considerations besides execution time? What if the user has a huge list and wants to use as little new allocated RAM as possible? I understand that it's immediately after the "How do I speed up my program?" question, and I think it's worth considering making the question more explicit what kind of efficiency we're talking about. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40344> _______________________________________
participants (4)
-
Andy Lester
-
Batuhan Taskaya
-
Dominik V.
-
Raymond Hettinger