0

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.

1
  • Have you tried locationDescriptions[0]['Identifier'] ? Commented Apr 26, 2021 at 21:04

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You could extract them with a list comprehension:

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

You allude to needing a list of numbers (count, min, max, etc...), in which case:

identifiers = [int(i['Identifier']) for i in locationDescriptions]

Comments

1

You can do

ids = [locationDescription['Identifier'] for locationDescription in locationDescriptions]

You will have a list of identifiers as a string.

Best regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.