Manuel Aráoz Manuel Aráoz - 1 year ago 76
Python Question

pythonic way to do something N times without an index variable?

Every day I love python more and more.

Today, I was writing some code like:

for i in xrange(N):

I had to do something N times. But each time didn't depend on the value of
(index variable).
I realized that I was creating a variable I never used (
), and I thought "There surely is a more pythonic way of doing this without the need for that useless index variable."

So... the question is: do you know how to do this simple task in a more (pythonic) beautiful way?

Answer Source

A slightly faster approach than looping on xrange(N) is:

import itertools

for _ in itertools.repeat(None, N):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download