Newbie Questions: Swithing from Perl to Python

Ian Bicking ianb at colorstudy.com
Sat Oct 25 23:23:51 EDT 2003


On Saturday, October 25, 2003, at 09:30 PM, Todd Stephens wrote:
> On Sat, 25 Oct 2003 22:07:13 -0400, Robert Brewer wrote:
>
>>>>> myhash = {'a': 1, 'b': 2, 'c': 3}
>>>>> myhash
>> {'a': 1, 'c': 3, 'b': 2}
>>>>> sortedKeys = myhash.keys()
>>>>> sortedKeys.sort()
>>>>> for eachKey in sortedKeys:
>> ... 	print "%s = %s\n" % (eachKey, myhash[eachKey]) ...
>
> Excellent.  I was laboring under the incorrect assumption that 
> dictionary
> keys could not be sorted.  Guess I have quite a bit more reading-up to 
> do
> on dictionaries.

The key (no pun intended) is that *dictionaries* can't be sorted.  
sortedKeys in this example is a list (a list of all the keys).  Lists 
can be sorted.

--
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org






More information about the Python-list mailing list