[Tutor] dictionaries

Christian Ebert blacktrash@gmx.net
Thu, 21 Feb 2002 18:03:05 +0100


Hello Tutors,

First of all: Compliments to everybody. IMO lurking on this
list - which I have done so far - is the best way to start
learning Python.

alan.gauld@bt.com at 12:11 on Thursday, February 21, 2002:

> for key in mydict.keys().sort():
>     # process mydict[key]

This doesn't work in one line for me. Because sort() sorts
_in place_?

Python 2.2 (#124, Dec 22 2001, 17:36:16)  [CW PPC GUSI2 THREADS GC]
Type "copyright", "credits" or "license" for more information.
MacPython IDE 1.0.1
>>> mydict = {1:'a',2:'b',3:'c'}
>>> for key in mydict.keys().sort():
... 	print mydict[key]
... 
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: iteration over non-sequence
>>> sortedkeys = mydict.keys().sort()
>>> sortedkeys
>>> 
>>> mykeys = mydict.keys()
>>> for key in mykeys.sort():
... 	print mydict[key]
... 
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: iteration over non-sequence
>>> mykeys.sort()
>>> for key in mykeys:
... 	print mydict[key]
... 
a
b
c
>>> 

or did I overlook something?

Christian
-- 
Hyperion in Frankfurt
www.tko.ch/blacktrash
www.schauspielfrankfurt.de