[Tutor] Is there a programmatic use for keys() and values()

Chris “Kwpolska” Warrick kwpolska at gmail.com
Sun Jun 16 19:54:31 CEST 2013


On Sun, Jun 16, 2013 at 7:25 PM, Jim Mooney <cybervigilante at gmail.com> wrote:
> On 15 June 2013 23:30, Dave Angel <davea at davea.name> wrote:
>
>>>> The sort() method doesn't work, but sorted does.
>
> How many times have I read you can't sort a dictionary in Python. Was
> I just misreading or was that true of older Pythons?

Dicts have no order:

>>> {'b': 'c', 'a': 'z'}
{'a': 'z', 'b': 'c'}

Moreover, the sorted() function sorts the keys only (something that
list() does anyway).  If you are looking for a way to have dicts with
an order of keys (and you don’t need that most of the time), look at
collection.OrderedDict.

--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html


More information about the Tutor mailing list