[Tutor] dictionary keys

Peter Otten __peter__ at web.de
Wed Apr 9 08:55:20 CEST 2014

Alex Kleider wrote:

> On 2014-04-08 14:34, Peter Otten wrote:
>> That's a change in Python 3 where dict.keys() no longer creates a list,
>> but
>> instead creates a view on the underlying dict data thus saving time and
>> space. In the rare case where you actually need a list you can
>> explicitly
>> create one with
>> ips = list(ipDic)

> Thanks, Peter, for this clarification.  I want to present the list
> sorted so probably this is the rare case of which you spoke where I
> would need to use l = list(myDict) rather than the view.

You can create and sort the list in a single step:

l = sorted(myDict)

More information about the Tutor mailing list