Getting specific data from list of dictionaries in Python using dot notation -
i have list of dictionaries , strings so:
listdict = [{'id':1,'other':2}, {'id':3,'other':4}, {'name':'some name','other':6}, 'some string']
i want list of ids (or other attributes) dictionaries via dot operator. so, given list list:
listdict.id [1,3] listdict.other [2,4,6] listdict.name ['some name']
thanks
python doesn't work way. you'd have redefine listdict
. built-in list type doesn't support such access. simpler way new lists this:
>>> ids = [d['id'] d in listdict if isinstance(d, dict) , 'id' in d] >>> ids [1, 3]
p.s. data structure seems awfully heterogeneous. if explain you're trying do, better solution can found.
Comments
Post a Comment