needs_eval = "((abc or def) and ghi)"
dict_group = {abc: ['gh@ab.com', 'ab@ab.com', 'ij@ab.com'], def: ['ab@ab.com', 'cd@ab.com', 'ef@ab.com'], ghi: ['cd@ab.com', 'ab@ab.com', 'kl@ab.com', 'gh@ab.com']}
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: ((['gh@ab.com', 'ab@ab.com', 'ij@ab.com'] | ['ab@ab.com', 'cd@ab.com', 'ef@ab.com']) & ['cd@ab.com', 'ab@ab.com', 'kl@ab.com', 'gh@ab.com'])
When i evaluate "needs_eval" i want the logical output "['cd@ab.com', 'ab@ab.com', 'gh@ab.com']"
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.