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?
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/