Newbie Questions: Swithing from Perl to Python

Todd Stephens huzzah at tampabay.rr.com
Sun Oct 26 00:28:24 EDT 2003


On Sat, 25 Oct 2003 23:16:35 -0400, Roy Smith wrote:

> Exactly.
> 
> keys = myDict.keys()
> keys.sort()
> for key in keys:
>    print key
 
Thanks for the info.  Knowing that the Python community prefers one
correct way to do something, can you explain to me how this is
different/incorrect? :

myD = {'x':4, 'k':2, 'r':3, 'e':1}
myL = list(myD)
myL.sort()
for x in myL:
   print "%s = %s" %(x, myD[x])

When run, it yields this:
e = 1
k = 2
r = 3
x = 4

I have tried this both ways, and I appear to get the same results.  Are
there situations where the method I have listed here would yield
unpredictable or unwanted results?  Or is this an area where Python
doesn't care?

-- 
Todd Stephens




More information about the Python-list mailing list