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

Markus Meskanen markusmeskanen at gmail.com
Thu Mar 30 05:18:53 EDT 2017


Hi Pythonistas,

yet again today I ended up writing:

d = [[0] * 5 for _ in range(10)]

And wondered, why don't we have a way to repeat other than looping over
range() and using a dummy variable? This seems like a rather common thing
to do, and while the benefit doesn't seem much, something like this would
be much prettier and more pythonic than using underscore variable:

d = [[0] * 5 repeat_for 10]

And could obviously be used outside of comprehensions too:

repeat_for 3:
    print('Attempting to reconnect...')
    if reconnect():
        break
else:
    print('Unable to reconnect :(')
    sys.exit(0)

I chose to use repeat_for instead of repeat because it's way less likely to
be used as a variable name, but obviously other names could be used like
loop_for or repeat_times etc.

Thoughts?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170330/4e54a9ae/attachment.html>


More information about the Python-ideas mailing list