Comparing dictionaries in Python lists
<p>I have two lists which contain dictionaries. Each dictionary has only one entry. I would like to check if a key in dictionary A (in list X) also exists in a dictionary in list Y. If this is the case, the key and the values belonging to it should be printed.</p>

<p>Example:</p>

<pre><code>listA = [{key1: value1}, {key2: value2}]
listB = [{key1: value3}, {key4: value4}]
</code></pre>

<p>In this case the output should be:</p>

<pre><code>key1: value1, value3
</code></pre>

<p>Thanks in advance.</p>

<p>A very simple way to do it would be:</p>

<pre><code>#!/usr/bin/env python

l1 = [{'1':"one"} , {'2':"two"}]
l2 = [{'3':"three"} , {'1':"one_too"}]

def cmp(l1,l2):
    for i in l1:    
        for j in l2:
            for (key1,value1),(key2,value2)  in zip(i.iteritems(),j.iteritems()):
                if key1==key2:
                    print key1+": "+value1+", "+value2
                    break

cmp(l1,l2)
</code></pre>

python list dictionary