pythonlistdictionarylambdagenerator-expression

Searching and Filtering in a list of dictionaries/nested dictionary python


Hi I'm having trouble filtering this list of dictionaries. What im trying to get to is the in the all_assets list and get to the 'attributes' dictionary and filter it by the value of (forexample) Background: Star. My current try is using the filter() and lambda function.

What I have so far is this:

all_assets = [{'dateListed': 58391,
  'id': '118572',
  'metadata': {'files': [],
               'mediaType': 'image/png',
               'name': 'The three',
               'tags': [{'right': '101 Galaxy'},
                        {'attributes': {'Background': 'Mars',
                                        'Body': 'Rough',
                                        'Face': 'Dumb',
                                        'Headwear': 'Helmet'}}],
               'thumbnail': 'something'},
  'Session': None,
  'police': 'pewpew',
  'verified': {'project': 'Thes', 'verified': True}},
 {'dateListed': 430298239,
  'id': '1191281',
  'metadata': {'files': [],
               'mediaType': 'image/png',
               'name': 'TheOne',
               'tags': [{'right': '101 Galaxy'},
                        {'attributes': {'Background': 'Star',
                                        'Body': 'Smooth',
                                        'Face': 'Fresh',
                                        'Headwear': 'Cap'}}],
               'thumbnail': 'something'},
  'Session': None,
  'police': 'pewpew',
  'verified': {'project': 'Thes', 'verified': True}}]

search_attribute = 'Background'
search_attribute_value = 'Star'

filtered = filter(lambda i: any(d.get(search_attribute, 'xxx') == search_attribute_value for d in i['metadata']['tags']['attributes']), all_assets)
# print(list(filtered))

The output should be since the search attribute is for star:

 {'dateListed': 430298239,
  'id': '1191281',
  'metadata': {'files': [],
               'mediaType': 'image/png',
               'name': 'TheOne',
               'tags': [{'right': '101 Galaxy'},
                        {'attributes': {'Background': 'Star',
                                        'Body': 'Smooth',
                                        'Face': 'Fresh',
                                        'Headwear': 'Cap'}}],
               'thumbnail': 'something'},
  'Session': None,
  'police': 'pewpew',
  'verified': {'project': 'Thes', 'verified': True}}]

Then what I want to do with the filtered list is printout some of the values like 'id', 'name' and 'headwear'.

    for x in filtered:
        id = (x.get('id'))
        name = (x.get('metadata')('name'))
        headwear = (x.get('metadata')('tags')('attributes')('Headwear'))
        print(f'ID: {id}')
        print(f'Name: {name}')
        print(f'Headwear: {headwear}')

Im trying to get the output to this:

ID: 1191281
Name: TheOne
Headwear: Cap

Im somewhat new to this field of python as I only know a bit about lambda functions. So sorry if its a dumb question or something.


Solution

  • I have coded a custom filter method:

    def customFilter(all_assets, search_attribute, search_attribute_value):
        res = []
        for asset in all_assets:
            if asset:
                attributes = asset["metadata"]["tags"][1]["attributes"]
                if attributes and search_attribute in attributes:
                    if search_attribute_value == attributes[search_attribute]:
                        res.append(asset)
        return res
    

    Lets test it:

    all_assets = [
        {
            "dateListed": 58391,
            "id": "118572",
            "metadata": {
                "files": [],
                "mediaType": "image/png",
                "name": "The three",
                "tags": [
                    {"right": "101 Galaxy"},
                    {
                        "attributes": {
                            "Background": "Mars",
                            "Body": "Rough",
                            "Face": "Dumb",
                            "Headwear": "Helmet",
                        }
                    },
                ],
                "thumbnail": "something",
            },
            "Session": None,
            "police": "pewpew",
            "verified": {"project": "Thes", "verified": True},
        },
        {
            "dateListed": 430298239,
            "id": "1191281",
            "metadata": {
                "files": [],
                "mediaType": "image/png",
                "name": "TheOne",
                "tags": [
                    {"right": "101 Galaxy"},
                    {
                        "attributes": {
                            "Background": "Star",
                            "Body": "Smooth",
                            "Face": "Fresh",
                            "Headwear": "Cap",
                        }
                    },
                ],
                "thumbnail": "something",
            },
            "Session": None,
            "police": "pewpew",
            "verified": {"project": "Thes", "verified": True},
        },
    ]
    
    search_attribute = "Background"
    search_attribute_value = "Star"
    
    print(customFilter(all_assets, search_attribute, search_attribute_value))
    

    Output:

    [{'dateListed': 430298239, 'id': '1191281', 'metadata': {'files': [], 'mediaType': 'image/png', 'name': 'TheOne', 'tags': [{'right': '101 Galaxy'}, {'attributes': {'Background': 'Star', 'Body': 'Smooth', 'Face': 'Fresh', 'Headwear': 'Cap'}}], 'thumbnail': 'something'}, 'Session': None, 'police': 'pewpew', 'verified': {'project': 'Thes', 'verified': True}}]