0

I have 2 list ..

a = [{'Work': 'R'}, {'Area': 'S0'}, {'Type': 'PIV'}, {'Disc': 'LA'}, {'L': 'A'}] 
b = [{'Key': '9', 'FileName': '123A.pdf', 'Code': '1', 'Type': 'PNHRG'}]

output -- [{'Key': '9', 'FileName': '123A.pdf', 'Code': '1', 'Type': 'PNHRG','Work': 'R','Area': 'S0','Type': 'PIV','Disc': 'LA','L': 'A'}]

I tried 'extend','append','insert' but did not get desired output. tried all most all list operations with loops too. I am not in position to change any kind of types for this.

Tried this solution too without luck. not sure where I am missing.

How do I make a flat list out of a list of lists?

Right leg
  • 16,080
  • 7
  • 48
  • 81
RRP
  • 25
  • 5
  • 4
    What is the problem? What are you trying to do? – Cow Jul 29 '22 at 07:39
  • Are you sure, that you want a `list` with just one `dict` in it? – Jacob Jul 29 '22 at 07:40
  • Yes, Jacob.. I want list with one dict in it. – RRP Jul 29 '22 at 07:42
  • 3
    So basically you have N lists, each with M dictionaries (with distinct keys?) and want to combine all those into a single dict? Why is that still wrapped in a list? – tobias_k Jul 29 '22 at 07:43
  • I am getting this from other sources ( different) and want to wrapped into a list. – RRP Jul 29 '22 at 07:44
  • It is hard to believe that you have tried all list operations if you do not provide any code that works but the output differs from the expected output. – Jacob Jul 29 '22 at 07:44
  • 2
    @RRP there is 0 benefit to having a dict wrapped into a single element list. – matszwecja Jul 29 '22 at 07:46
  • @matszwecja There is sort of a benefit if that's the format you have to use for reasons you can't influence (but otherwise I agree). – tobias_k Jul 29 '22 at 07:51

2 Answers2

4

You can use a doubly-nested dictionary comprehension, iterating over all the items in all the dicts in all the lists. Assuming multiple dicts in b might not be necessary, but makes it easier.

>>> a = [{'Work': 'R'}, {'Area': 'S0'}, {'Type': 'PIV'}, {'Disc': 'LA'}, {'L': 'A'}] 
>>> b = [{'Key': '9', 'FileName': '123A.pdf', 'Code': '1', 'Type': 'PNHRG'}]
>>> [{k: v for lst in (a, b) for d in lst for k, v in d.items()}]
[{'Work': 'R', 'Area': 'S0', 'Type': 'PNHRG', 'Disc': 'LA', 'L': 'A', 'Key': '9', 'FileName': '123A.pdf', 'Code': '1'}]

Addendum: If there are any keys that appear in more than one dictionary, as is the case with "type" here, the value from the last dictionary that was iterated in the comprehension will be used, i.e. b here. If you need the value from a, do ... for lst in (b, a).... In your expected output, that key appears twice, but that is not possible unless you change the format, as in the other answer.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

Extra from the answer provided by tobias_k:

If you want to combine both lists of dictionaries (and more when you have it) as a dictionary with a list of values in the same key. You can do this.

from collections import defaultdict

mydict = defaultdict(list)

for i in (a+b):
    for key, value in i.items():
        mydict[key].append(value)
        

mydict
Out[32]: 
defaultdict(list,
            {'Work': ['R'],
             'Area': ['S0'],
             'Type': ['PIV', 'PNHRG'],
             'Disc': ['LA'],
             'L': ['A'],
             'Key': ['9'],
             'FileName': ['123A.pdf'],
             'Code': ['1']})