I am trying to get all the values in my dictionary list that have a value greater than 'string value'. I've tried several of the iterations below plus several more. What is the proper way to do this with a string value of a dictionary other than on the key?
import requests
s = requests.Session()
lookupurl = "https://rxnav.nlm.nih.gov/REST/rxclass/allClasses.json?classTypes=ALL" #REST API call for all class types
lookupdata = s.get(lookupurl).json()
lookup1 = lookupdata['rxclassMinConceptList']['rxclassMinConcept']
#lookup = [k for v,k in lookup1['classid'] if v >= 'D016849'] TypeError: list indices must be integers or slices, not str
#lookup = [k for v,k in lookup1[0:]['classid'] if v >= 'D016849'] TypeError: list indices must be integers or slices, not str
#lookup = [k for v,k in lookup1[0]['classid'] if v >= 'D016849'] KeyError: 'classid'
#lookup = [v for k,v in lookup1['classid'] if k >= 'D016849'] TypeError: list indices must be integers or slices, not str
#lookup = [v for k,v in lookup1[0:]['classid'] if k >= 'D016849'] KeyError: 'classid'
#lookup = [v for k,v in lookup1.classid() if k >= 'D016849'] AttributeError: 'list' object has no attribute 'classid'
#lookup = [v for k,v in lookup1.classid() if k >= 'D016849'] AttributeError: 'list' object has no attribute 'items'
#lookup = dict(filter(lambda lookup1: lookup1['classid'] > 'D016849', lookup1['classid'])) TypeError: list indices must be integers or slices, not str
#lookup = dict(filter(lambda lookup1: lookup1[0:]['classid'] > 'D016849', lookup1[0:]['classid'])) TypeError: list indices must be integers or slices, not str
print(lookup)
My expected results would return only the classid's >= 'D016849'
CodePudding user response:
Try this:
lookup = [item for item in lookup1 if item['classId'] >= 'D016849']
I think the main problem is classid
should be classId
. Another issue is that you seem to be accessing classId
on lookup1
in many of your attempts. But actually, it's a property of each element in lookup1
, not on lookup1
itself.