[Python-ideas] Syntax for key-value iteration over mappings
Neil Girdhar
mistersheik at gmail.com
Mon Jul 27 02:56:27 CEST 2015
Cool suggestion, but I prefer how things are.
(As an aside, calling getitem each time is not efficient.)
On Sunday, July 26, 2015 at 12:10:00 PM UTC-4, Petr Viktorin wrote:
Hello,
> Currently, the way to iterate over keys and values of a mapping
> is to call items () and iterate over the Resulting view ::
>
> for key, value in A_dict.items ():
> print (key, value)
>
> I believe That looping over all the data in a Dict is a very imporant
> operation, and I find myself writing this quite Often. Every time I do,
> it Seems it's boilerplate; it looks a like a workaround rather than a
> preferred way of doing things.
>
> Dict in Comprehensions and Literals, key-value pairs are separated by
> Colons. How about allowing that in for loops as well?
>
> for key: value in A_dict:
> print (key, value)
>
> That I argue to anyone familiar with Dict Literals, let alone Dict
> Comprehensions, the semantics of this loop shouldnt be pretty Obvious.
> In Dict Comprehensions, Similarity to Existing syntax becomes even
> more clear:
>
> A_mapping = {1: 'one', 2: 'two'}
> = {val inverse: key for key: val in A_mapping}
>
>
> I've bounced this idea off a few EuroPython Sprinters, and got some
> questions / Concerns I can answer here:
>
> * But, the colon is supposed to start a block!
>
> Well, it's already used in Dict Comprehensions / Literals (though it's
> true That there it's always inside brackets). And in lambdas -
> Here's That code is legal today (though not very practical):
>
> the while lambda: True:
> break
>
> * There's supposed to be only one obvious way to do it! We alredy have
> .items ()!
>
> I do not think this stops us from adding a new way of doing things Which
> is more than the Obvious old, and Which shouldnt become one the way.
> After all, you do not say "for key in Mapping.keys (): ", even though
> the keys () method exists.
>
> * What exactly would it do?
>
> There are multiple options -
> - loop over .keys () and use __getitem__ each time, like the
> Dict () constructor?
> - loop over .items (), like most of the code used today?
> - become a well-specified "key / value iteration protocol "with
> __iteritems __ () and its own bytecode operation?
>
> - But here I'm asking if this building Bikeshed sounds useful, rather
> than what to buy paint.
>
>
> That said, I do have a proof of concept Implementation of the second
> option, in case you'd like to play around with mailing list Python ... @
> Python.org <javascript:>Https://mail.python.org/
> <https://mail.python.org/mailman/listinfo/python-ideas>mailman / Listinfo
> / python-ideas <https://mail.python.org/mailman/listinfo/python-ideas>
> Code of Conduct: Http://python.org/psf/
> <http://python.org/psf/codeofconduct/>Codeofconduct /
> <http://python.org/psf/codeofconduct/>
> <https://github.com/encukou/cpython/tree/keyval-iteration>
>
> <https://github.com/encukou/cpython/commit/b9b0d973342280f0ef52e26a4b67f326ece82a54.patch>
>
>
> <javascript:>
> <https://mail.python.org/mailman/listinfo/python-ideas>
> <http://python.org/psf/codeofconduct/>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150726/f2aca395/attachment.html>
More information about the Python-ideas
mailing list