What others have answered is tangential. Nobody doubts there are places where you want to repeat something N times. Just to add an example, if you are parsing something nested by parentheses or perhaps HTML, and you reach a point where you have seen N opening symbols and start seeing a closing symbols, you might want to swallow the next N-1 such symbols before moving on to whatever comes next. Yes, I know that can be more complex.
But in many such cases, people do not just count lines or whatever, but actively examine each line. This is especially true where it is valid to have say a comment embedded or other things that make the count uncertain. Again, what language does something simpler? Example, in R, is this much better? for(n_times in 1:6) {print("hi")} [1] "hi" [1] "hi" [1] "hi" [1] "hi" [1] "hi" [1] "hi" Using R, again, you can make a repeat function that calls your designated function with a fizxed argument N times: repetito = function(n, func, arg) { for (i in 1:n) { func(arg) } } repetito(6, print, "hi") [1] "hi" [1] "hi" [1] "hi" [1] "hi" [1] "hi" [1] "hi" Python allows similar ways: >>> def repetito(n, func, args) : for _ in range(n): func(args) >>> repetito(6, print, "hi") hi hi hi hi hi hi You can modify these ideas to having no arguments or multiple arguments or even varying numbers of arguments with varying numbers of positional and keyword arguments. You need to be careful at times to make sure that the calls are not evaluated once but each time. I suspect the request boils down to wanting yet another keyword or two added to a language that does not need more bloat. -----Original Message----- From: Python-list <python-list-bounces+avigross=verizon....@python.org> On Behalf Of DL Neil via Python-list Sent: Monday, December 23, 2019 7:27 PM To: Python <python-list@python.org> Subject: Re: Most elegant way to do something N times On 24/12/19 1:04 PM, Chris Angelico wrote: > On Tue, Dec 24, 2019 at 10:45 AM Marco Sulla <elbar...@gmail.com> 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. >>> >> >> ??? Excuse me, but why you needed to call the same function SIX >> times? This seems to me not elegant in primis. >> >> Can you give us a practical example? Taking the top/bottom six from a sorted list of occurrences. eg in Accounts Payable, a clerk might be given the task of 'chasing' the six largest debtors (people who owe us money), as today's work assignment/priority. > File parsing. You read a section header and want to ignore that > section, so you ignore the next 15 lines. (just to be cheeky to @Chris) Perhaps better as a Finite State Machine, with one state being 'seeking section header'. 1 can we guarantee that the 'magic constant' of 15 will always apply? 2 presumably the total routine will involve more than identifying a single header and skipping (only) that section. -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list