sorting a dictionary?

Tony Meyer t-meyer at ihug.co.nz
Tue Mar 22 04:09:10 CET 2005


> Thank you scott, but 'sorted' itself is not a python
> library function.  So I assume by
> "sorted(mydict.items()", you meant the object of
> mydict.items() which has been sorted, right?

It's a builtin as of Python 2.4:

Python 2.4.1c1 (#63, Mar 10 2005, 10:36:41) [MSC v.1310 32 bit (Intel)] on
win32

Type "help", "copyright", "credits" or "license" for more information.
>>> sorted
<built-in function sorted>

Python 2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> sorted
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'sorted' is not defined

So you can use it if you have 2.4, but not otherwise (but the other
solutions posted will work).

=Tony.Meyer




More information about the Python-list mailing list