Another dict method...

Stephen Hansen news at myNOSPAM.org
Sat May 19 07:18:06 CEST 2001


... Dictionaries aren't sorted, and I can't imagine an iterator is going to
go through the trouble of sorting them. Even with iterators, if you wish to
iterate over a dictionary in sorted order, you'll have to do what you do
now, I believe... that is, extract the .keys() and sort them, then iterate
over them.

    for key in dict.keys().sort()

for instance.

Then again, I only know about iterators what i've been reading here, so may
bewrong.. but i can't imagine I am in this one instance. :)

--Stephen
(replace 'NOSPAM' with 'seraph' to respond in email)

"Emile van Sebille" <emile at fenx.com> wrote in message
news:9e4uda$13fu0$1 at ID-11957.news.dfncis.de...
> Yes.  Can it retrieve them in sorted order?
>
> I also see a clear method.  Is there an advantage in using it over simply
> reassigning to a new {} ?
>
> --
>
> Emile van Sebille
> emile at fenx.com
>
> ---------
> "Tim Peters" <tim.one at home.com> wrote in message
> news:mailman.990239607.10076.python-list at python.org...
> > [Magnus Lie Hetland]
> > > Anyone who sees the use of a dict.peekitem() which works
> > > like dict.popitem() but doesn't actually remove an item
> > > from the dictionary? Could be useful for things like
> > > this (cf. earlier post about minimum of dict):
> > > ...
> > > Just a thought. The following seems a bit ... odd:
> > >
> > >     k1, v1 = dict.popitem() # Get an item
> > >     dict[k1] = v1           # Put it back
> >
> > Under current CVS Python you can do
> >
> >     k1, v1 = dict.iteritems().next()
> >
> >
>
>





More information about the Python-list mailing list