from glom import glom, T
from glom.core import Val
target = [
{"firstname": 'boka', 'lastname':'raton'},
{"firstname": 'cape', 'lastname':'town'}
]
spec = ([{
'fname':'firstname',
'index': Val('0')
}])
r = glom(target, spec)
print(r)
Is it possible to capture the index while iterating over a list of dictionaries using glom?
The current output is:
[{'fname': 'boka', 'index': '0'}, {'fname': 'cape', 'index': '0'}].
But I'd like to have the following:
[{'fname': 'boka', 'index': 0}, {'fname': 'cape', 'index': 1}]
Solved it by enumerating over the items and performing glom on one dictionary at a time.
Used the
Val
to evaluate the wrapped value ofindex