Revert converting str.join(listcomp) to str.join(generator)
See original GitHub issueConsider reverting the listcomp->generator for the str.join()
case, it’s faster to use a list comprehension than a generator in this case:
$ python -m timeit '"y".join("x" for _ in range(1000000))'
5 loops, best of 5: 40.6 msec per loop
$ python -m timeit '"y".join(["x" for _ in range(1000000)])'
10 loops, best of 5: 29.6 msec per loop
This is because str.join()
constructs a list internally and short-circuits if passed an already created list.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
python - Joining strings. Generator or list comprehension?
The call to PySequence_Fast converts the seq argument into a list if it wasn't a list or tuple already. So, the two versions...
Read more >Convert list to string in python using join() / reduce() / map()
""" Convert list to string, by joining all item in list with given separator. Returns the concatenated string """. return seperator.
Read more >How to Use Python's Join() on a List of Objects (Not Strings)?
The most Pythonic way to concatenate a list of objects is the expression ''.join(str(x) for x in lst) that converts each object to...
Read more >Python | Convert List of String List to String List - GeeksforGeeks
In this, we join the numbers using join and construct a string integers. ... join(). List comprehension is used to iterate through the...
Read more >Python's map(): Processing Iterables Without a Loop
You'll also learn how to use list comprehension and generator expressions to replace map() in a ... Transforming Iterables of Strings With Python's...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Having tried this out, it looks like even that one’s slower:
I’d suggested this feature, and it seems that in several cases it induces a small performance hit, so…I’m sorry about that
@asottile reckon it’s worth removing this rewrite altogether?
@MarcoGorelli Thanks for making my eyes actually work, sorry for the noise.