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.
I have coded a custom filter method:
Lets test it:
Output: