[Python-ideas] Way to repeat other than "for _ in range(x)"

Mark Dickinson dickinsm at gmail.com
Fri Mar 31 04:23:26 EDT 2017


On Thu, Mar 30, 2017 at 10:18 AM, Markus Meskanen
<markusmeskanen at gmail.com> wrote:
> And wondered, why don't we have a way to repeat other than looping over
> range() and using a dummy variable?

If it's the assignment to a dummy variable that bothers you, the
language already has a way around this:

Python 3.6.0 (default, Jan  9 2017, 12:18:47)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import functools, itertools
>>> times = functools.partial(itertools.repeat, ())
>>> ["spam" for () in times(4)]
['spam', 'spam', 'spam', 'spam']

Look Ma, no dummy variables!

-- 
Mark


More information about the Python-ideas mailing list