[Python-3000] Iterators for dict keys, values, and items == annoying :)

Guido van Rossum guido at python.org
Sat Apr 1 07:31:51 CEST 2006


On 3/31/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Not a good idea, since the long history of "list.sort()" encourages people to
> think of the sort() method as an inplace operation, which it wouldn't be on a
> view.

Right. This is degenerating quickly. :-(

> "sorted()", on the other hand, already creates a new object. The only
> downside is that Py2.x & Py3k compatible code would look like:
>
>    keys = sorted(d.keys())
>
> which is likely to create the list *twice* in Py2.x.

So write

  keys = sorted(d)

which should work in both versions. :-)

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list