I have a nested dictionary in my redis store and need to find all records where key_2 == ""
then return value in key_3
. All key_3
values will then be used to run mysql query to get key_2
value and update the redis store.
[
...
task_44903: {"key_1": 44903, "key_2": "", "key_3": 1}
task_44229: {"key_1": 44229, "key_2": 4, "key_3": 2}
...
]
My current way of achieving this is
keys = r.keys(pattern='task_*')
key_3 = set()
for key in keys:
values = r.get(key).decode('utf-8')
values = ast.literal_eval(values)
if values['key_2'] == '':
key_3.add(values['key_3'])
Is there a more concise way of achieving this?
I'm not sure if this is what you wanted.
data = [
{'task_44903': {"key_1": 44903, "key_2": "", "key_3": 1}},
{'task_44229': {"key_1": 44229, "key_2": "", "key_3": 1}},
{'task_44230': {"key_1": 44229, "key_2": "", "key_3": 2}}
]
set([ v['key_3'] for item in data for _, v in item.items() if v['key_2']==""])
Output
{1, 2}