Most elegant way to do something N times

Richard Damon Richard at Damon-Family.org
Sun Dec 22 16:23:31 EST 2019


On 12/22/19 3:34 PM, Batuhan Taskaya wrote:
> I encounter with cases like doing a function 6 time with no argument, or
> same arguments over and over or doing some structral thing N times and I
> dont know how elegant I can express that to the code. I dont know why but I
> dont like this for _ in range(n): do() thing. Any suggestions?

For me, the statement

for _ in range(n): do()

is the most straight forward and clearest expression of that idea.

If you don't like the _, you can always use some 'worthless' variable to
get rid of the special _ syntax.

You could of course use something like a while loop to build this, but
in my mind that is just making things less clear.

-- 
Richard Damon



More information about the Python-list mailing list