[Tutor] Sorting a dictionary

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Nov 4 09:31:13 CET 2005



> ser_port = {'4401': 'ds-srvr', \
>  '5427': 'sco-peer-tta', \
>  '4446': 'n1-fwp', \
>  '3734': 'synel-data', \
>  '4447': 'n1-rmgmt', \
>  '5745': 'fcopy-server', \
>  '5990': 'wbem-exp-https', \
>  '4026': 'as-debug', \
>  '3724': 'battlenet'}

Hi Johan,

Just as a quick side note: all those continuation characters '\' at the
end of those lines are unneccessary --- Python knows that there's more to
the line because of it's in the middle of a dictionary definition.  So you
can clean things up slightly by doing:

###########################
ser_port = {
 '4401': 'ds-srvr',
 '5427': 'sco-peer-tta',
 '4446': 'n1-fwp',
 '3734': 'synel-data',
 '4447': 'n1-rmgmt',
 '5745': 'fcopy-server',
 '5990': 'wbem-exp-https',
 '4026': 'as-debug',
 '3724': 'battlenet',
}
###########################

Everything lines up nicely, and there's a little less line noise.  *grin*

We can even leave a trailing comma on the last key-value pair, just to
make adding more entries into the list easier, as we've done for the
'3724' entry.


I hope this helps!



More information about the Tutor mailing list