I'm sure a question like this exists somewhere on here but I can't seem to find it. I have the following dictionary called query
:
query = {'day': 0, 'item': 'Chipotle'}
And a mapping dictionary:
day_mapping = {0:2, 1:3, 2:4, 3:5, 4:6, 5:7, 6:1}
I need to override query
to produce the following dictionary based on the mapping:
query = {'day': 2, 'item': 'Chipotle'}
Note query
may or may not contain the key day
so the following should return no error:
query = {'item': 'Chipotle'}
This is what I've come up with so far but there's gotta be a better way using list comprehension or something:
for k, v in query.items():
if k == 'day':
query[k] = day_mapping[v]
The reason for this is the numeric representation for days is different for python's calendar
library and MongoDB's aggregation
framework. Kinda annoying but I'm sure this happens all over the place.