473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reversing a dict?

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.

Jun 27 '08 #1
3 967
On May 6, 5:20 pm, krumbleb...@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.

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]
Jun 27 '08 #2
Thanks all!!

kb.

Jun 27 '08 #3
On May 6, 4:24*pm, cokofree...@gmail.com wrote:
On May 6, 5:20 pm, krumbleb...@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.
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]
Better:

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

--
Paul Hankin
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
4
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart...
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
11
by: sandravandale | last post by:
I can think of several messy ways of making a dict that sets a flag if it's been altered, but I have a hunch that experienced python programmers would probably have an easier (well maybe more...
15
by: Cruella DeVille | last post by:
I'm trying to implement a bookmark-url program, which accepts user input and puts the strings in a dictionary. Somehow I'm not able to iterate myDictionary of type Dict{} When I write print...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
16
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it...
1
by: | last post by:
Hello all, I have a question which might be simple or need some work around. I want to do something like this. My class/instance has a dict as a property. I want the instance to catch the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.