[Python-ideas] Syntax for key-value iteration over mappings
Joonas Liik
liik.joonas at gmail.com
Mon Jul 27 17:25:19 CEST 2015
> Here's a crazy alternative: Generalize it to subsume the common use of
> enumerate(). Iterate over a dict thus:
>
> for name:obj in globals():
> # do something with the key and/or value
>
> And iterate over a list, generator, or any other simple linear iterable thus:
>
> for idx:val in sys.argv:
> # do something with the arg and its position
>
> In other words, the two-part iteration mode gives you values *and
> their indices*. If an object declares its own way of doing this, it
> provides the keys and values itself; otherwise, the default is
> equivalent to passing it through enumerate, so you'll get sequential
> numbers from zero.
>
Well it may well be crazy but somewhere deep inside i actually quite like it..
Certainly more than a special syntax that only works on dicts.., and
its quite a common use case imo.
More information about the Python-ideas
mailing list