[Tutor] Loop in pre-defined blocks

Michael Selik michael.selik at gmail.com
Mon Jun 13 18:20:08 EDT 2016


On Mon, Jun 13, 2016 at 1:33 PM Ek Esawi <esawiek at gmail.com> wrote:

> Here is a beginner code that might work for you. Best of luck.   EK
>
> b=[12, 20, 35]
>
> for i in range(len(b)):
>      if i==0:
>           c=0
>      else:
>           c=b[i-1]
>      for j in range(c, b[i]):
>           print(i+1,j+1)
>


If you want to go a little further, you can use zip and enumerate.

    py> stops = [3, 5, 9]
    py> starts = [0] + stops[:-1]
    py> for i, (start, stop) in enumerate(zip(starts, stops)):
    ...     for j in range(start, stop):
    ...             print(i, j)
    ...
    0 0
    0 1
    0 2
    1 3
    1 4
    2 5
    2 6
    2 7
    2 8


More information about the Tutor mailing list