#8267: Use sorted() to get a sorted list of dict keys.

This commit is contained in:
Georg Brandl 2010-10-15 15:31:09 +00:00
parent a81eae1fd7
commit 44c3ceb8ac
2 changed files with 5 additions and 5 deletions

View File

@ -431,9 +431,9 @@ function like this::
print "-- I'm sorry, we're all out of", kind print "-- I'm sorry, we're all out of", kind
for arg in arguments: print arg for arg in arguments: print arg
print "-" * 40 print "-" * 40
keys = keywords.keys() keys = sorted(keywords.keys())
keys.sort() for kw in keys:
for kw in keys: print kw, ":", keywords[kw] print kw, ":", keywords[kw]
It could be called like this:: It could be called like this::

View File

@ -481,8 +481,8 @@ using a non-existent key.
The :meth:`keys` method of a dictionary object returns a list of all the keys The :meth:`keys` method of a dictionary object returns a list of all the keys
used in the dictionary, in arbitrary order (if you want it sorted, just apply used in the dictionary, in arbitrary order (if you want it sorted, just apply
the :meth:`sort` method to the list of keys). To check whether a single key is the :func:`sorted` function to it). To check whether a single key is in the
in the dictionary, use the :keyword:`in` keyword. dictionary, use the :keyword:`in` keyword.
Here is a small example using a dictionary:: Here is a small example using a dictionary::