needs_eval = "((abc or def) and ghi)"
dict_group = {abc: ['[email protected]', '[email protected]', '[email protected]'], def: ['[email protected]', '[email protected]', '[email protected]'], ghi: ['[email protected]', '[email protected]', '[email protected]', '[email protected]']}
for k,v in dict_group.iteritems():
str_v=str(v[0])
needs_eval = needs_eval.replace("and", "&").replace("or", "|").replace(k,str_v)
#needs_eval = re.sub(k,v[0],needs_eval)
print(list(eval(needs_eval)))
O/p i get: ((['[email protected]', '[email protected]', '[email protected]'] | ['[email protected]', '[email protected]', '[email protected]']) & ['[email protected]', '[email protected]', '[email protected]', '[email protected]'])
When i evaluate "needs_eval" i want the logical output "['[email protected]', '[email protected]', '[email protected]']"
I am converting the dict "value" into a string before substituting it into the "needs_eval" string because "replace or re.sub" only passes strings.