Francis Francis - 3 months ago 6
Python Question

Create a new list from two dictionaries (case insensitive)

This is a question about Python. I have the following list of dictionaries:

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"}
]


and a dictionary I wanted to compare with:

dictA = {"t": 1, "tid": 2, "gtm": "goofy"}


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

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"},
]


How do I compare the two dicts in a case-insensitive way?

Answer

You'd have to test each dictionary value manually:

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)]

This produces the 3 matches you are looking for:

>>> 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"}
...         ]
>>> dictA = {"t": 1, "tid": 2, "gtm": "goofy"}
>>> [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'}]
>>> from pprint import pprint
>>> 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}]

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

listB = [dict(dictA, id=d['id'], gtm=d['gtm']) for d in listA if test(dictA, d)]

This creates a copy of dictA and adds in the id and gtm keys from the matching dictionary:

>>> [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'}]
>>> 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}]
Comments