Search code examples
pythondictionaryordereddictionary

Order of values when extracting Python dict to list


I am trying to get all values (except corresponding to a particular key) from a list of dicts by doing this:

fv = [[v for (k,v) in d.iteritems() if k is not 'xKey'] for d in someDict] 

where someDict is like:

[{xKey:0.1,yKey:0.2,zKey:0.3},{yKey:0.9,xKey:0.7,zKey:0.4}...]

I know a dict doesn't have an inherent order. But my list of lists fv needs to have the values in order. I am thinking about sorting the dict on key and then doing what I just did. But is that guaranteed to work?

I know using OrderedDict is an option but it also seems to have inferior performance as compared to dict, which would be a concern for me since my dictionary is typically going to have huge amount of data.

Update : When I say I need values in order, they don't really have to be sorted.What I mean is I need to be able to retrieve the list of values in a fixed deterministic order each time.In the above example, I always want to get [[0.2,0.3],[0.9,0.4]] although it may not be a sorted order per se. Sorting would enforce one deterministic order.What I really care about is maintaining the position of values in the final list.e.g. Value of yKey must always be the first value in each list, the value of zKey must always be the second value in each list and so on even though ykey, zkey etc may be in any order in the dictionary.


Solution

  • If you know the list of possible keys that your dicts may contain, the following solution might work for you:

    allkeys = ...  # might be known; or obtained from available dicts by union;
                   # 'xKey' can be removed at this stage to simplify the list
                   # comprehension that follows
    sortedKeys = sorted(allKeys)
    list_of_values = [[d.get(k) for k in sortedKeys if k in d]
                                                       for d in list_of_dicts] 
    

    It might be slower than iteritems though. The if k in d part can be removed if all the dicts contain the same set of keys.