For Loop Dilema [python-list]

Ian Kelly ian.g.kelly at gmail.com
Sun Feb 25 19:49:05 EST 2018


On Sun, Feb 25, 2018 at 11:19 AM,  <arya.kumar2494 at gmail.com> wrote:
> Why we don’t use:
>
> for _ in _ in _
>
> Instead of
>
> for _ in _:
>         for _ in _:
>
> Ex:
>
> Names = ["Arya","Pupun"]
>
> for name in Names:
>    for c in name:
>        print(c)
>
> instead use:
>
> for c in name in Names:
>         print(c)

It doesn't seem very intuitive (doesn't follow proper English
phrasing, for instance) and I don't think it's a common enough
situation to warrant adding a special syntax for it. But if you really
want it, you could use something like this:

def double_for(iterable):
    for outer in iterable:
        yield from outer

for c in double_for(Names):
    print(c)

But I don't think this is any clearer than making the loops explicit.



More information about the Python-list mailing list