Reversing a dict?

castironpi at gmail.com castironpi at gmail.com
Tue May 6 23:52:00 EDT 2008


On May 6, 10:20 am, krumbleb... at gmail.com wrote:
> Hi - further to my earlier query regarding partial matches (which with
> all your replies enabled me to advance my understanding, thanks), I
> now need to reverse a dict.
>
> I know how to reverse a list (with the reverse method - very handy),
> but it doesn't seem possible to reverse a dict.

It is not.  Dictionaries provide constant time add and remove, -not-
order.  Are you sorting based on key or value?

If you want a sorted state and sorted remove, use the 'bisect' module.

> I suspect what I need to do is somehow go from:
>
> thelist=list(thedict)
> thelist.reverse()
> thedict=dict(thelist)
>
> Does anyone know how to convert / or reverse a dict?
>
> thanks
>
> kb.




More information about the Python-list mailing list