Reversing a dict?

K

krumblebunk

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.

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.
 
C

cokofreedom

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.

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.

Issue 1: A dictionary is not ordered so cannot be reversed, as is.

Saw something like this though:

info = {"PHP":"17th May",
"Perl":"15th June",
"Java":"7th June",
"Python":"26th May",
"Tcl":"12th July",
"MySQL":"24th May"}

topics = info.keys()
topics.sort()
topics.reverse()

for topic in topics:
print "Next",topic,"course starts",info[topic]
 
J

Jeremy Sanders

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.

There is no guaranteed order to the items stored in a dictionary. They can
and will move around as the dict is modified. Have a look at
diveintopython:

http://www.diveintopython.org/getting_to_know_python/dictionaries.html

You'll have to store your keys in a list or tuple to keep them ordered.

Jeremy
 
I

Ian Kelly

P

Paul Hankin

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.
I suspect what I need to do is somehow go from:

Does anyone know how to convert / or reverse a dict?

kb.

Issue 1: A dictionary is not ordered so cannot be reversed, as is.

Saw something like this though:

info = {"PHP":"17th May",
       "Perl":"15th June",
       "Java":"7th June",
       "Python":"26th May",
       "Tcl":"12th July",
       "MySQL":"24th May"}

topics = info.keys()
topics.sort()
topics.reverse()

for topic in topics:
   print "Next",topic,"course starts",info[topic]

Better:

for topic, when in sorted(topics.iteritems(), reverse=True):
print 'Next %s course starts %s' % (topic, when)
 
C

castironpi

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Dict comprehension help 0
Dict comp help 0
Reversing a linked list 116
Reversing a string 23
write to a file two dict() 2
dict order 9
parsing string into dict 3
building a dict 12

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top