perymerdeka
perymerdeka

Reputation: 823

Compare keys value on two list of dict

I'm trying to compare the contents of a dictionary from 2 lists of dict, I have data like this

data1 = [
    {
        "name": "MoreDependentsInd",
        "description": "Form 1040-SP Indicator",
        "data_type": "CheckboxType",
        "xpath": "",
    },
    {
        "name": "DependentFirstNm",
        "description": "Form 1040-SR Indicator",
        "data_type": "PersonNameControlType",
        "xpath": "",
    },
]

data2 = [
    {
        "id": 29,
        "kind": "IN",
        "container": 1,
        "content": "null",
        "uid": "IRS 1040-DependentDetail",
        "title": "DependentDetail",
        "display_name": "null",
        "description": "null",
        "extra_info": {
            "kind": "null",
            "include": {
                "fields": [
                    {
                        "kind": "PersonFirstNameType",
                        "name": "DependentFirstNm",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent First Name",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "PersonLastNameType",
                        "name": "DependentLastNm",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Last Name",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "PersonNameControlType",
                        "name": "DependentNameControlTxt",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Name Control",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "null",
                        "name": "IdentityProtectionPIN",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent/Qualifying Child Identity Protection PIN",
                                "line_number": "(2)",
                            }
                        },
                        "restriction": {"rule": "null"},
                    },
                    {
                        "kind": "null",
                        "name": "DependentRelationshipCd",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Relationship Code",
                                "line_number": "(3)",
                            }
                        },
                        "restriction": {
                            "rule": {
                                "kind": "TextType",
                                "enumeration": [
                                    "SON",
                                    "DAUGHTER",
                                    "STEPCHILD",
                                    "FOSTER CHILD",
                                    "BROTHER",
                                    "SISTER",
                                    "STEPBROTHER",
                                    "STEPSISTER",
                                    "HALF BROTHER",
                                    "HALF SISTER",
                                    "GRANDCHILD",
                                    "NIECE",
                                    "NEPHEW",
                                    "PARENT",
                                    "GRANDPARENT",
                                    "AUNT",
                                    "UNCLE",
                                    "OTHER",
                                    "NONE",
                                ],
                            }
                        },
                    },
                    {
                        "kind": "PersonFirstNameType",
                        "name": "DependentFirstNm",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent First Name",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "PersonLastNameType",
                        "name": "DependentLastNm",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Last Name",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "PersonNameControlType",
                        "name": "DependentNameControlTxt",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Name Control",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "null",
                        "name": "IdentityProtectionPIN",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent/Qualifying Child Identity Protection PIN",
                                "line_number": "(2)",
                            }
                        },
                        "restriction": {"rule": "null"},
                    },
                    {
                        "kind": "null",
                        "name": "DependentRelationshipCd",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Relationship Code",
                                "line_number": "(3)",
                            }
                        },
                        "restriction": {
                            "rule": {
                                "kind": "TextType",
                                "enumeration": [
                                    "SON",
                                    "DAUGHTER",
                                    "STEPCHILD",
                                    "FOSTER CHILD",
                                    "BROTHER",
                                    "SISTER",
                                    "STEPBROTHER",
                                    "STEPSISTER",
                                    "HALF BROTHER",
                                    "HALF SISTER",
                                    "GRANDCHILD",
                                    "NIECE",
                                    "NEPHEW",
                                    "PARENT",
                                    "GRANDPARENT",
                                    "AUNT",
                                    "UNCLE",
                                    "OTHER",
                                    "NONE",
                                ],
                            }
                        },
                    },
                    {
                        "kind": "PersonFirstNameType",
                        "name": "DependentFirstNm",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent First Name",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "PersonLastNameType",
                        "name": "DependentLastNm",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Last Name",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "PersonNameControlType",
                        "name": "DependentNameControlTxt",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Name Control",
                                "line_number": "(1)",
                            }
                        },
                        "restriction": "null",
                    },
                    {
                        "kind": "null",
                        "name": "IdentityProtectionPIN",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent/Qualifying Child Identity Protection PIN",
                                "line_number": "(2)",
                            }
                        },
                        "restriction": {"rule": "null"},
                    },
                    {
                        "kind": "null",
                        "name": "DependentRelationshipCd",
                        "annotation": {
                            "documentation": {
                                "description": "Dependent Relationship Code",
                                "line_number": "(3)",
                            }
                        },
                        "restriction": {
                            "rule": {
                                "kind": "TextType",
                                "enumeration": [
                                    "SON",
                                    "DAUGHTER",
                                    "STEPCHILD",
                                    "FOSTER CHILD",
                                    "BROTHER",
                                    "SISTER",
                                    "STEPBROTHER",
                                    "STEPSISTER",
                                    "HALF BROTHER",
                                    "HALF SISTER",
                                    "GRANDCHILD",
                                    "NIECE",
                                    "NEPHEW",
                                    "PARENT",
                                    "GRANDPARENT",
                                    "AUNT",
                                    "UNCLE",
                                    "OTHER",
                                    "NONE",
                                ],
                            }
                        },
                    },
                ],
                "reference": "null",
            },
            "annotation": "null",
            "max_occurs": "100",
            "min_occurs": "0",
            "restriction": "null",
        },
        "instruction": "null",
        "created_at": "2022-04-26T09:53:36.426118Z",
        "updated_at": "2022-04-26T09:53:36.426203Z",
    },
    {
        "id": 30,
        "kind": "IN",
        "container": 1,
        "content": "null",
        "uid": "IRS 1040-MoreDependentsInd",
        "title": "MoreDependentsInd",
        "display_name": "null",
        "description": "null",
        "extra_info": {
            "kind": "CheckboxType",
            "include": "null",
            "annotation": {
                "documentation": {
                    "description": "More Dependents Indicator",
                    "line_number": "null",
                }
            },
            "max_occurs": "null",
            "min_occurs": "0",
            "restriction": "null",
        },
        "instruction": "null",
        "created_at": "2022-04-26T09:53:36.427552Z",
        "updated_at": "2022-04-26T09:53:36.427647Z",
    },
]

for i in data1:
    for j in data2:
        if i["name"] == j["title"]:
            print("success")

the result is a success, but in my code, there is a deficiency, if it processes a lot of data then the performance will feel slow, how to fix it? and what if the value such as the data type is in a key that we don't know is located?

for example, I search value data_type from data1 I compare it with CheckboxType

"extra_info": {
            "kind": "CheckboxType",
            "include": "null",
            "annotation": {
                "documentation": {
                    "description": "More Dependents Indicator",
                    "line_number": "null",
                }
            },

from data2 on index 1 data2[1] but the value CheckboxType is also exist in another key on data2 in another index

Thanks!

Upvotes: 0

Views: 61

Answers (1)

Samwise
Samwise

Reputation: 71562

You can do the test in O(n) instead of O(n^2) by building sets of name and title (which is O(n)) and then taking their intersection (which is also O(n) now that they're sets):

>>> {i["name"] for i in data1} & {i["title"] for i in data2}
{'MoreDependentsInd'}

Non-empty sets are truthy so you can use this in a conditional if you don't care about what the intersection is:

>>> if {i["name"] for i in data1} & {i["title"] for i in data2}:
...     print("success")
...
success

If you want to look at all string values without having to know that name and title are the intersecting keys, use another generator expression to put all string values into the two sets:

>>> {v for i in data1 for v in i.values() if isinstance(v, str)} & {v for i in data2 for v in i.values() if isinstance(v, str)}
{'MoreDependentsInd'}

Upvotes: 1

Related Questions