Skip to content
Advertisement

How to parse a list of dictionaries in Python 3

I’m using python(requests) to query an API. The JSON response is list of dictionaries, like below:

locationDescriptions = timeseries.publish.get('/GetLocationDescriptionList')['LocationDescriptions']


print(locationDescriptions[0])

{'Name': 'Test',
 'Identifier': '000045',
 'UniqueId': '3434jdfsiu3hk34uh8',
 'IsExternalLocation': False,
 'PrimaryFolder': 'All Locations',
 'SecondaryFolders': [],
 'LastModified': '2021-02-09T06:01:25.0446910+00:00',}

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:

identifiers = [item['Identifier'] for item in locationDescriptions]

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement