Get the dictionary on a list where there is a specific key value pairs

Viewed 110

Assuming i have this dictionary inside a list:

 [
    {
        "Name": "Person",
        "Confidence": 97.56156921386719,
        "Instances": [
            {
                "BoundingBox": {
                    "Width": 0.6137702465057373,
                    "Height": 0.9175498485565186,
                    "Left": 0.22297996282577515,
                    "Top": 0.0739903450012207
                },
                "Confidence": 94.51961517333984
            },
            {
                "BoundingBox": {
                    "Width": 0.46570318937301636,
                    "Height": 0.6405649781227112,
                    "Left": 0.11447866261005402,
                    "Top": 0.34079012274742126
                },
                "Confidence": 82.2153549194336
            }
        ],
        "Parents": []
    },
    {
        "Name": "Human",
        "Confidence": 97.56156921386719,
        "Instances": [],
        "Parents": []
    },
    {
        "Name": "Clothing",
        "Confidence": 91.08417510986328,
        "Instances": [],
        "Parents": []
    }
    }]

How can I retrieve the dictionary with key value "Name":"Clothing" only? is it possible to retrieve it from the dictionary? The Key "Clothing" may appear in any part of the list.

    {
        "Name": "Clothing",
        "Confidence": 91.08417510986328,
        "Instances": [],
        "Parents": []
    }
  
4 Answers

You can use the following code to get every item that has Name of Clothing:

names = [
    {
        "Name": "Person",
        "Confidence": 97.56156921386719,
        "Instances": [
            {
                "BoundingBox": {
                    "Width": 0.6137702465057373,
                    "Height": 0.9175498485565186,
                    "Left": 0.22297996282577515,
                    "Top": 0.0739903450012207
                },
                "Confidence": 94.51961517333984
            },
            {
                "BoundingBox": {
                    "Width": 0.46570318937301636,
                    "Height": 0.6405649781227112,
                    "Left": 0.11447866261005402,
                    "Top": 0.34079012274742126
                },
                "Confidence": 82.2153549194336
            }
        ],
        "Parents": []
    },
    {
        "Name": "Human",
        "Confidence": 97.56156921386719,
        "Instances": [],
        "Parents": []
    },
    {
        "Name": "Clothing",
        "Confidence": 91.08417510986328,
        "Instances": [],
        "Parents": []
    }
]

for name in names:
    if name['Name'] == 'Clothing':
        print(name)

If you want it to be short you can use the following code:

x = [y for y in names if name['Name'] == 'Clothing']

You can use this simple list comprehension:

[print(dictionary) for dictionary in lst if 'Name' in dictionary.keys() and dictionary['Name'] == 'Clothing']

Output:

{'Name': 'Clothing', 'Confidence': 91.08417510986328, 'Instances': [], 'Parents': []}

Here is the full code:

lst =  [
    {
        "Name": "Person",
        "Confidence": 97.56156921386719,
        "Instances": [
            {
                "BoundingBox": {
                    "Width": 0.6137702465057373,
                    "Height": 0.9175498485565186,
                    "Left": 0.22297996282577515,
                    "Top": 0.0739903450012207
                },
                "Confidence": 94.51961517333984
            },
            {
                "BoundingBox": {
                    "Width": 0.46570318937301636,
                    "Height": 0.6405649781227112,
                    "Left": 0.11447866261005402,
                    "Top": 0.34079012274742126
                },
                "Confidence": 82.2153549194336
            }
        ],
        "Parents": []
    },
    {
        "Name": "Human",
        "Confidence": 97.56156921386719,
        "Instances": [],
        "Parents": []
    },
    {
        "Name": "Clothing",
        "Confidence": 91.08417510986328,
        "Instances": [],
        "Parents": []
    }
]

[print(dictionary) for dictionary in lst if 'Name' in dictionary.keys() and dictionary['Name'] == 'Clothing']

The .get(Key, defaultValue) can be used here.

.get("Name", "") --> Returns an empty string if the key Name doesn't exist in the dictionary. doc .get("Name") --> Returns None if Name doesn't exist.

So .get("Name") == "Clothing" is the criteria for selection of a dictionary.

And use filter() to choose only those that satisfy the criteria.

Putting it all together

filter(lambda x: x.get("Name") == "Clothing", mylist)

filter returns an iterator, so you could pass that to list to create a full list. If you just want to iterate over it, no need to call list.

So,

list(filter(lambda x: x.get("Name") == "Clothing", mylist))

Assuming your list is called my_list, then this should give you what you want:

my_list_new = [x for x in my_list if ("Name", "Clothing") in x.items()]

Related