<div dir="ltr"><font><font><font><font><font><font>Cool suggestion, but I prefer how things are.</font></font></font></font></font></font><div><font><font><font><font><br></font></font></font></font></div><div><font><font><font><font>(As an aside, calling getitem each time is not efficient.)</font></font></font></font><div><font><font><br></font></font></div><div><font><font class=""> </font><font class="">On Sunday, July 26, 2015 at 12:10:00 PM UTC-4, Petr Viktorin wrote:</font></font><br><br><font></font><blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><font><font>Hello, </font></font><br><font><font>Currently, the way to iterate over keys and values ​​of a mapping </font></font><br><font><font>is to call items () and iterate over the Resulting view ::</font></font><p><font><font>    for key, value in A_dict.items (): </font></font><br><font><font>        print (key, value)</font></font></p><p><font><font>I believe That looping over all the data in a Dict is a very imporant </font></font><br><font><font>operation, and I find myself writing this quite Often. </font><font>Every time I do, </font></font><br><font><font>it Seems it's boilerplate; </font><font>it looks a like a workaround rather than a </font></font><br><font><font>preferred way of doing things.</font></font></p><p><font><font>Dict in Comprehensions and Literals, key-value pairs are separated by </font></font><br><font><font>Colons. </font><font>How about allowing that in for loops as well?</font></font></p><p><font><font>    for key: value in A_dict: </font></font><br><font><font>        print (key, value)</font></font></p><p><font><font>That I argue to anyone familiar with Dict Literals, let alone Dict </font></font><br><font><font>Comprehensions, the semantics of this loop shouldnt be pretty Obvious. </font></font><br><font><font>In Dict Comprehensions, Similarity to Existing syntax becomes even </font></font><br><font><font>more clear:</font></font></p><p><font><font>    A_mapping = {1: 'one', 2: 'two'} </font></font><br><font><font>    = {val inverse: key for key: val in A_mapping}</font></font></p><p><br><font><font>I've bounced this idea off a few EuroPython Sprinters, and got some </font></font><br><font><font>questions / Concerns I can answer here:</font></font></p><p><font><font>* But, the colon is supposed to start a block!</font></font></p><p><font><font>Well, it's already used in Dict Comprehensions / Literals (though it's </font></font><br><font><font>true That there it's always inside brackets). </font><font>And in lambdas - </font></font><br><font><font>Here's That code is legal today (though not very practical):</font></font></p><p><font><font>    the while lambda: True: </font></font><br><font><font>        break</font></font></p><p><font><font>* There's supposed to be only one obvious way to do it! </font><font class="">We alredy have .items ()!</font></font></p><p><font><font class="">I do not think this stops us from adding a new way of doing things Which </font></font><br><font><font class="">is more than the Obvious old, and Which shouldnt become one the way. </font></font><br><font><font class="">After all, you do not say "for key in Mapping.keys (): ", even though </font></font><br><font><font class="">the keys () method exists.</font></font></p><p><font><font>* What exactly would it do?</font></font></p><p><font><font>There are multiple options - </font></font><br><font><font>- loop over .keys () and use __getitem__ each time, like the </font></font><br><font><font>  Dict () constructor? </font></font><br><font><font>- loop over .items (), like most of the code used today? </font></font><br><font><font>- become a well-specified "key / value iteration protocol "with </font></font><br><font><font>  __iteritems __ () and its own bytecode operation?</font></font></p><p><font><font>- But here I'm asking if this building Bikeshed sounds useful, rather </font></font><br><font><font>than what to buy paint.</font></font></p><p><br><font><font>That said, I do have a proof of concept Implementation of the second </font></font><br><font><font>option, in case you'd like to play around with </font><font>mailing list </font><a href="javascript:" target="_blank" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;"><font>Python ... @ Python.org </font></a><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\46sa\75D\46sntz\0751\46usg\75AFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\46sa\75D\46sntz\0751\46usg\75AFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;"><font>Https://mail.python.org/ </font></a><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\46sa\75D\46sntz\0751\46usg\75AFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\46sa\75D\46sntz\0751\46usg\75AFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;"><font>mailman / Listinfo / python-ideas</font></a><font> Code of Conduct: </font><a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;"><font>Http://python.org/psf/ </font></a><a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;"><font>Codeofconduct /</font></a></font><br><font></font><a href="https://github.com/encukou/cpython/tree/keyval-iteration" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fencukou%2Fcpython%2Ftree%2Fkeyval-iteration\46sa\75D\46sntz\0751\46usg\75AFQjCNFnYcidRQdgfuVyzghfE7_6eConyg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fencukou%2Fcpython%2Ftree%2Fkeyval-iteration\46sa\75D\46sntz\0751\46usg\75AFQjCNFnYcidRQdgfuVyzghfE7_6eConyg';return true;"><font></font><font></font></a><br><font></font><a href="https://github.com/encukou/cpython/commit/b9b0d973342280f0ef52e26a4b67f326ece82a54.patch" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fencukou%2Fcpython%2Fcommit%2Fb9b0d973342280f0ef52e26a4b67f326ece82a54.patch\46sa\75D\46sntz\0751\46usg\75AFQjCNFidtOGkSK6-wV76dgnEPPJGz3Fng';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fgithub.com%2Fencukou%2Fcpython%2Fcommit%2Fb9b0d973342280f0ef52e26a4b67f326ece82a54.patch\46sa\75D\46sntz\0751\46usg\75AFQjCNFidtOGkSK6-wV76dgnEPPJGz3Fng';return true;"><font></font><font></font><font></font><font></font></a><br><font></font><font></font><br><font></font><br><a href="javascript:" target="_blank" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;"><font></font></a><br><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\46sa\75D\46sntz\0751\46usg\75AFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\46sa\75D\46sntz\0751\46usg\75AFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;"><font></font><font></font></a><br><font></font><a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;"><font></font><font></font></a></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p></blockquote></div></div></div>