for c in condition:
    if c != ')':
        stack.append(c)
else:
    d = c
contents = []
while d != '(':
    contents.insert(0, d)
d = stack.pop()
contents.insert(0, d)
s = ''.join(contents)
print(s)
stack.append(s)
Python nicest way to take boolean expression string apart