Compare two list of dictionaries and create a new list with all keys and values in python
<pre><code>a = [{'a':{'a':1}}, {'b':{'b':1}}, {'c':{}}]
b = [{'a':{'a':1,'b':0}}, {'b':{'a':1}}]
</code></pre>

<p>Here I have two list of dict. I want to compare these and generate a new list like this:</p>

<pre><code>newlist = [{'a':{'a':1,'b':0}}, {'b':{'a':1,'b':1}}, {'c':{'a':0,'b':0}}]
</code></pre>

<pre><code>A_list = [{'a':{'A':1}}, {'b':{}}]
B_list = [{'b': {'B': 1}}, {'a': {'A': 0}}]
list_with_all_keys = [{'a':{}}, {'b':{}}, {'c':{}}]
new_lod = [{k:{'A':(1 if k in A_list and 'A' in A_list[k] and A_list[k]['A'] is 1 else 0), 
               'B':(1 if k in B_list and 'B' in B_list[k] and B_list[k]['B'] is 1 else 0)}} 
           for k in [list(n.keys())[0] for n in list_with_all_keys]]
</code></pre>

<p><code>new_lod</code> is then <code>[{'a': {'B': 0, 'A': 0}}, {'b': {'B': 0, 'A': 0}}, {'c': {'B': 0, 'A': 0}}]</code></p>

<p>Note, that I added some upper-case letters, to clarify whats happening, it also work if you only use lower case letters <code>a</code>and <code>b</code> instead of <code>A</code>/<code>A_list</code> and <code>B</code>/<code>B_list</code> respectively.</p>

<p><strong>Sidenote</strong></p>

<p>I assume you do not get duplicate keys in the first level of dictionaries in the list. I think this is a correct assumption, since the required output would collapse these duplicate keys in the first level dictionaries. Thus you can skip the list and use one dictionary instead of a list of dictionaries. This slightly simplifies the expressions. But depending on your application there might be even better (speak easier) ways to compare or/and combine two lists, dictionaries or sets.</p>

<pre><code>A_dict = {'a':{'A':1}, 'b':{}}
B_dict = {'b': {'B': 1}, 'a': {'A': 0}}
dict_with_all_keys = {'a':{}, 'b':{}, 'c':{}}
# a dictionary solution with no list
new_dict = {k:{'A':(1 if k in A_dict and 'A' in A_dict[k] and A_dict[k]['A'] is 1 else 0), 
               'B':(1 if k in B_dict and 'B' in B_dict[k] and B_dict[k]['B'] is 1 else 0)} 
            for k in dict_with_all_keys}
</code></pre>

python list dictionary