Pythonic way to separate operators and operands in an expression
<p>I am trying to separate the operators (including parentheses) and the operands in an expression. For example given an expression</p>

<pre><code>expr = "(32+54)*342-(4*(3-9))"
</code></pre>

<p>I am trying to get</p>

<pre><code>['(', '32', '+', '54', ')', '*', '342', '-', '(', '4', '*', '(', '3', '-', '9', ')', ')']
</code></pre>

<p>Here is the code that I wrote. Is there a better way of doing it in python.</p>

<pre><code>l = list(expr)
n = ''
expr = []
try:
  for c in l:
    if c in string.digits:
      n += c
    else:
      if n != '':
        expr.append(n)
        n = ''
      expr.append(c)
finally:
  if n != '':
    expr.append(n)
</code></pre>
