I’m using python(requests) to query an API. The JSON response is list of dictionaries, like below:
JavaScript
x
13
13
1
locationDescriptions = timeseries.publish.get('/GetLocationDescriptionList')['LocationDescriptions']
2
3
4
print(locationDescriptions[0])
5
6
{'Name': 'Test',
7
'Identifier': '000045',
8
'UniqueId': '3434jdfsiu3hk34uh8',
9
'IsExternalLocation': False,
10
'PrimaryFolder': 'All Locations',
11
'SecondaryFolders': [],
12
'LastModified': '2021-02-09T06:01:25.0446910+00:00',}
13
I’d like to extract 1 field (Identifier) as a list for further analysis (count, min, max, etc.) but I’m having a hard time figuring out how to do this.
Advertisement
Answer
Python has a syntax feature called “list comprehensions”, and you can do something like:
JavaScript
1
2
1
identifiers = [item['Identifier'] for item in locationDescriptions]
2
Here is a small article that gives you more details, and also shows an alternate way using map
. And here is one of the many resources detailing list comprehensions, should you need it.