views:

297

answers:

6

Hey! Every day I love python more and more.

Today, I was writing some code like:

for i in xrange(N):
    do_something()

I had to do something N times. But each time didn't depend on the value of i (index variable). I realized that I was creating a variable I never used (i), 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?

Thanks in advance!

Manuel

+9  A: 

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

import itertools

for _ in itertools.repeat(None, N):
    do_something()
Alex Martelli
How much faster? Is there still a difference in Python 3.1?
Hamish Grubijan
@Hamish: My test with 2.6 says 32% faster (23.2 us vs 17.6 us for N=1000). But that is a really time time anyways. I would default to the OP's code because it is more immediately readable (to me).
Mike Boers
That's good to know about the speed. I certainly echo Mike's sentiment about the OP's code being more readable.
Wayne Werner
@Wayne, I guess habit is really very powerful -- except for the fact that you're used to it, why else would "count up from 0 to N-1 [[and completely ignore the count]] each time performing this count-independent operation" be intrinsically any clearer than "repeat N times the following operation"...?
Alex Martelli
+7  A: 

Use the _ variable, as I learned when I asked this question

GreenMatt
+2  A: 

The _ is the same thing as x however it's just a python idiom that's used to indicate an identifier that you don't intend to make use of at all. In python these identifiers don't takes memory or allocate space like variables do in other languages. It's easy to forget that. They're just names that point to objects, in this case an integer on each iteration.

Khorkrak
+3  A: 

since function is first-class citizen, you can write small wrapper (from Alex answers)

def repeat(f, N):
    for _ in itertools.repeat(None, N): f()

then you can pass function as argument.

aaa
I wonder what the penalty is ...
Hamish Grubijan
@Hamish: Almost nothing. (17.8 us per loop under the same conditions as the timings for Alex's answer, for a 0.2 us difference).
Mike Boers
+2  A: 

I just use for _ in range(n), it's straight to the point. It's going to generate the entire list for huge numbers in Python 2, but if you're using Python 3 it's not a problem.

Longpoke
+1  A: 

Assume that you've defined do_something as a function, and you'd like to perform it N times. Maybe you can try the following:

todos = [do_something] * N  
for doit in todos:  
    doit()
Cox Chen
Hahahaha loved your answer! Thanks!
Manuel
Sure. Let's not just call the function a million times, let's allocate a list of a million items too. If the CPU is working, shouldn't also the memory get stressed a little?The answer cannot be characterized as definitely “not useful” (it's showing a different, functioning approach) so I can't downvote, but I disagree and I'm totally opposed to it.
ΤΖΩΤΖΙΟΥ