I have a Python list called “results” that has dictionaries as its values:
JavaScript
x
15
15
1
results = [
2
{
3
'postingStatus': 'Active',
4
'postEndDate': '1601683199000',
5
'boardId': '_internal',
6
'postStartDate': '1591084714000)'
7
},
8
{
9
'postingStatus': 'Expired',
10
'postEndDate': '1601683199000',
11
'boardId': '_external',
12
'postStartDate': '1591084719000)'
13
}
14
]
15
How would I create a list that gets all the values from the dictionary where the ‘boardID’ value is ‘_internal’ (but ignores the dictionary where ‘boardID’ is ‘_external’)? As an end result, I’m hoping for a list with the following contents:
JavaScript
1
2
1
['Active','1601683199000','_internal','1591084714000']
2
Advertisement
Answer
You can use list-comprehension:
JavaScript
1
3
1
out = [v for d in results for v in d.values() if d["boardId"] == "_internal"]
2
print(out)
3
Prints:
JavaScript
1
2
1
['Active', '1601683199000', '_internal', '1591084714000)']
2