473,385 Members | 1,863 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.

Reorder dictionary in pyrhon according to a list of values!!!

15
Let us consider a dictionary:
Expand|Select|Wrap|Line Numbers
  1. sample_dict={1:'r099',2:'g444',3:'t555',4:'f444',5:'h666'}
  2.  
I want to re-order this dictionary in an order specified by a list containing the order of the dictionary keys that I desire. Let us say the desired order list is:
Expand|Select|Wrap|Line Numbers
  1. desired_order_list=[5,2,4,3,1]
So, I want my dictionary to appear like this:
Expand|Select|Wrap|Line Numbers
  1. {5:'h666',2:'g444',4:'f444',3:'t555',1:'r099'}
How do I achieve this in the least complex way possible?
May 8 '11 #1
1 3821
dwblas
626 Expert 512MB
Dictionaries are in hash order always so can not be re-ordered. There is an ordered dictionary object in collections which you might want to use, or you can just use a list of tuples.
Expand|Select|Wrap|Line Numbers
  1. sample_dict={1:'r099',2:'g444',3:'t555',4:'f444',5:'h666'}
  2. output_list = [(key, sample_dict[key]) for key in (5, 4, 2, 1 ,3) if key in sample_dict]
  3. print output_list 
May 8 '11 #2

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

Similar topics

0
by: uv507 | last post by:
Hello, I have found a way to easily reorder rows of my listview. But it only works in small icons mode. When I do my reordering (drag and drop in my own list) in large icons mode, the item that...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
10
by: Ben | last post by:
Hello... I have set up a dictionary into whose values I am putting a list. I loop around and around filling my list each time with new values, then dumping this list into the dictionary. Or so I...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
0
by: Greg Corradini | last post by:
Hello All, I'm attempting to create multiple dictionaries at once, each with unique variable names. The number of dictionaries i need to create depends on the length of a list, which was returned...
14
by: erikcw | last post by:
Hi, I'm trying to turn o list of objects into a dictionary using a list comprehension. Something like entries = {} = d.id] for d in links]
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
2
by: geetamadhavi | last post by:
Hi All, I am a java developer got a php project from the office how to create a list.values in the list should come from the database MYSQL what to do for this please help me out as early as...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.