'Accessing items in odict_items [closed]
I have odict_items that is looking like below. Here is the printed output of Ordereddict
items that I have
odict_items([('S.No', '1'), ('Name', 'Ventura'), ('Location', 'Ph'), ('OL', 'ML'), ('Tech', 'IT'), ('Value', 223)])
odict_items([('S.No', '4'), ('Name', 'Ventura'), ('Location', 'Ph'), ('OL', 'ML'), ('Tech', 'IT'), ('Value', 223)])
odict_items([('S.No', '15'), ('Name', 'Ventura'), ('Location', 'Ph'), ('OL', 'ML'), ('Tech', 'IT'), ('Value', 223)])
How can I access the S.No
key from each item and group the values 1, 4, 15?
Solution 1:[1]
I have to assume here that you have something similar to the following situation at some point:
ods = [ OrderedDict(<your data>), OrderedDict(<your data>), OrderedDict(<your data>) ]
for d in ods:
print(d.items())
If you still have the dictionaries these came from, you can and should access the value using d['S.No']
.
If for some reason you do not still have the OrderedDict
s themselves, you can make a function to get the value:
def get_sno(od_items):
for item in od_items:
if item[0] == 'S.No':
return item[1]
return None
Used as such:
snos = []
for d in ods:
snos.append(get_sno(d.item()) # preferably snos.append(d['S.No']) instead
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Quinn Mortimer |