8

I have a data structure consisting of mixed dictionaries and lists. I am trying to unpack this so as to get tuples of the keys and all sub-values for each key.

I am working with list comprehensions, but just not getting it to work. Where am I going wrong?

I saw many other answers about unpacking a list-of-lists (e.g. 1,2), but could not find an example where a single key unpacks against multiple sub-values.

  • desired output --> [('A',1,2),('B',3,4)]
  • actual output --> [('A',1), ('A',2), ('B',3), ('B',4)]

code:

dict_of_lists = {'A':[{'x':1},{'x':2}], 'B':[{'x':3},{'x':4}] }
print [(key,subdict[subkey],) for key in dict_of_lists.keys() for subdict in dict_of_lists[key] for subkey in subdict.keys()]
Community
  • 1
  • 1
Roberto
  • 2,054
  • 4
  • 31
  • 46

2 Answers2

7

When list comprehensions become

  • long
  • unclear/hard to read
  • and most importantly, do not work

ditch them and go with the manual for loop(s) every time:

Python 2.x

def unpack(d):
    for k, v in d.iteritems():
        tmp = []
        for subdict in v:
            for _, val in subdict.iteritems():
                tmp.append(val)
        yield (k, tmp[0], tmp[1])


print list(unpack({'A':[{'x':1},{'x':2}], 'B':[{'x':3},{'x':4}] }))

Python 3.x

def unpack(d):
        for k, v in d.items():
            tmp = []
            for subdict in v:
                for _, val in subdict.items():
                    tmp.append(val)
            yield (k, *tmp) # stared expression used to unpack iterables were
                            # not created yet in Python 2.x                

print(list(unpack({'A':[{'x':1},{'x':2}], 'B':[{'x':3},{'x':4}] })))

Output:

[('A', 1, 2), ('B', 3, 4)]
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
2

Loop through the list of dicts and get only the values. Then combine with the dict key.

>>> for k,L in dict_of_lists.iteritems():
...   print tuple( [k]+[v for d in L for v in d.values()])

('A', 1, 2)
('B', 3, 4)

If you need a one liner:

>>> map(tuple, ([k]+[v for d in L for v in d.values()] for k,L in dict_of_lists.iteritems()))

[('A', 1, 2), ('B', 3, 4)]
helloV
  • 50,176
  • 7
  • 137
  • 145
  • Thanks! This helped me understand the syntax better, and why I was not getting it to work in the first place. – Roberto Nov 16 '16 at 01:17