By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,332 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,332 IT Pros & Developers. It's quick & easy.

Reversing elements in a dictionary?

P: 1
For example, let's say I have something like
Expand|Select|Wrap|Line Numbers
  1. letters = {'a':'01', 'b':'02', 'c':'03', 'd':'04', 'e':'05', 'f':'06', 'g':'07', 'h':'08', 'i':'09', 'j':'10', 'k':'11', 'l':'12', 'm':'13', 'n':'14', 'o':'15', 'p':'16', 'q':'17', 'r':'18', 's':'19', 't':'20', 'u':'21', 'v':'22', 'w':'23', 'x':'24', 'y':'25', 'z':'26'}            
  2.  
Is there any built in function that could reverse the elements to
Expand|Select|Wrap|Line Numbers
  1. letters = {'01':'a', '02':'b'}
and so on?
Aug 5 '08 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 849
You can use dict.fromkeys() to do this, as it makes the keys the list passed as the first parameter and the values the optional second parameter:

Expand|Select|Wrap|Line Numbers
  1. letters = letters.fromkeys(letters.values(), letters.keys())
  2.  
Aug 5 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.