0

I had a pattern of numbers that repeats mod 72, so on a hunch I got the idea to use the following Python syntax:

for i in (x+t for x in range(k-k%72,80000000001,72) for t in (1,3,7,9,15,19,25,27,33,39,43,51,55,57,63)):

I think the first for clause in the generator expression executes for one cycle, then the entire second one executes, then the next one in the first, then the entire second, etc., because the numbers come out in the correct order. It seems to produce the result I was looking for, but is this documented? I tried searching on Google with no conclusive results. Is it a feature, or am I just exploiting a quirk in Python? I know that if clauses are allowed, but I didn't know that multiple for clauses were too.

[Edit] Upon reading the link provided, I see I'm not the first to ask such a question. I also forgot that a generator expression and a list comprehension are essentially the same syntax, and so if I had tried "list comprehension" instead, maybe I'd have found it too.

Brian J. Fink
  • 585
  • 1
  • 4
  • 9
  • 2
    They're like nested `for` loops. – Barmar Jun 21 '23 at 23:01
  • 3
    The linked duplicate has an accepted answer which goes in a lot of detail about comprehension constructs and generator expressions and how it works. Also, this is absolutely documented, [here](https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-comprehension) and [here](https://docs.python.org/3/reference/expressions.html#generator-expressions) – juanpa.arrivillaga Jun 21 '23 at 23:05
  • Sorry, when I composed this post I thought I had possibly discovered something new. I'm going to leave my question right where it is, though. Maybe someone else with a stupid question such as mine will find it in a search and click the link and not have to be humiliated like I was. – Brian J. Fink Jun 21 '23 at 23:17

0 Answers0