Create a new list from two dictionaries (case insensitive)
<p>This is a question about Python. I have the following list of dictionaries:</p>

<pre><code>listA = [
          {"t": 1, "tid": 2, "gtm": "Goofy", "c1": 4, "id": "111"},
          {"t": 3, "tid": 4, "gtm": "goofy", "c1": 4, "c2": 5, "id": "222"},
          {"t": 1, "tid": 2, "gtm": "GooFy", "c1": 4, "c2": 5, "id": "333"},
          {"t": 5, "tid": 6, "gtm": "GoOfY", "c1": 4, "c2": 5, "id": "444"},
          {"t": 1, "tid": 2, "gtm": "GOOFY", "c1": 4, "c2": 5, "id": "555"}
        ]
</code></pre>

<p>and a dictionary I wanted to compare with:</p>

<pre><code>dictA = {"t": 1, "tid": 2, "gtm": "goofy"}
</code></pre>

<p>I wanted to create a list of dicts that match all the items in <strong>dictA</strong> from <strong>listA</strong> and to include the "id" field as well:</p>

<pre><code>listB = [
          {"t": 1, "tid": 2, "gtm": "Goofy", "id": "111"},
          {"t": 1, "tid": 2, "gtm": "GooFy", "id": "333"},
          {"t": 1, "tid": 2, "gtm": "GOOFY", "id": "555"},
        ]
</code></pre>

<p>How do I compare the two dicts in a case-insensitive way?</p>

<p>You'd have to test each dictionary value manually:</p>

<pre><code>def test(d1, d2):
    """Test if all values for d1 match case-insensitively in d2"""
    def equal(v1, v2):
        try:
            return v1.lower() == v2.lower()
        except AttributeError:
            # not an object that supports .lower()
            return v1 == v2
    try:
        return all(equal(d1[k], d2[k]) for k in d1)
    except KeyError:
        # d2 is missing a key, not a match
        return False

listB = [d for d in listA if test(dictA, d)]
</code></pre>

<p>This produces the 3 matches you are looking for:</p>

<pre><code>&gt;&gt;&gt; listA = [
...           {"t": 1, "tid": 2, "gtm": "Goofy", "c1": 4, "id": "111"},
...           {"t": 3, "tid": 4, "gtm": "goofy", "c1": 4, "c2": 5, "id": "222"},
...           {"t": 1, "tid": 2, "gtm": "GooFy", "c1": 4, "c2": 5, "id": "333"},
...           {"t": 5, "tid": 6, "gtm": "GoOfY", "c1": 4, "c2": 5, "id": "444"},
...           {"t": 1, "tid": 2, "gtm": "GOOFY", "c1": 4, "c2": 5, "id": "555"}
...         ]
&gt;&gt;&gt; dictA = {"t": 1, "tid": 2, "gtm": "goofy"}
&gt;&gt;&gt; [d for d in listA if test(dictA, d)]
[{'tid': 2, 'c1': 4, 'id': '111', 't': 1, 'gtm': 'Goofy'}, {'gtm': 'GooFy', 't': 1, 'tid': 2, 'c2': 5, 'c1': 4, 'id': '333'}, {'gtm': 'GOOFY', 't': 1, 'tid': 2, 'c2': 5, 'c1': 4, 'id': '555'}]
&gt;&gt;&gt; from pprint import pprint
&gt;&gt;&gt; pprint(_)
[{'c1': 4, 'gtm': 'Goofy', 'id': '111', 't': 1, 'tid': 2},
 {'c1': 4, 'c2': 5, 'gtm': 'GooFy', 'id': '333', 't': 1, 'tid': 2},
 {'c1': 4, 'c2': 5, 'gtm': 'GOOFY', 'id': '555', 't': 1, 'tid': 2}]
</code></pre>

<p>but these include the extra keys. If you must have only certain keys, pick those keys in a new dictionary:</p>

<pre><code>listB = [dict(dictA, id=d['id'], gtm=d['gtm']) for d in listA if test(dictA, d)]
</code></pre>

<p>This creates a copy of <code>dictA</code> and adds in the <code>id</code> and <code>gtm</code> keys from the matching dictionary:</p>

<pre><code>&gt;&gt;&gt; [dict(dictA, id=d['id'], gtm=d['gtm']) for d in listA if test(dictA, d)]
[{'tid': 2, 'id': '111', 't': 1, 'gtm': 'Goofy'}, {'tid': 2, 'id': '333', 't': 1, 'gtm': 'GooFy'}, {'tid': 2, 'id': '555', 't': 1, 'gtm': 'GOOFY'}]
&gt;&gt;&gt; pprint(_)
[{'gtm': 'Goofy', 'id': '111', 't': 1, 'tid': 2},
 {'gtm': 'GooFy', 'id': '333', 't': 1, 'tid': 2},
 {'gtm': 'GOOFY', 'id': '555', 't': 1, 'tid': 2}]
</code></pre>

python dictionary