title: Parsing a list of tokens in Python
body: <p>GOAL: I have a list of tokens. Whenever there is a section of tokens surrounded by brackets, such as {t1,t2,etc.}, I need to make that into a new sublist. An example is provided below of my desired result.</p>
  
  <hr>
  
  <p>parse:['(facto)', 'dup', 'length', '/n', 'exch', 'def', '/fact', '{', '0', 'dict', 'begin', '/n', 'exch', 'def', 'n', '2', 'lt', '{', '1', '}', '{', 'n', '1', 'sub', 'fact', 'n', 'mul', '}', 'ifelse', 'end', '}', 'def', 'n', 'fact', 'stack']) </p>
  
  <p>returns: 
   ['(facto)', 'dup', 'length', '/n', 'exch', 'def', '/fact', [0, 'dict', 'begin', '/n', 'exch', 'def', 'n', 2, 'lt', [1], ['n', 1, 'sub', 'fact', 'n', 'mul'], 'ifelse', 'end'], 'def', 'n', 'fact', 'stack']</p>
  
  <hr>
  
  <p>Here is my code so far:</p>
  
  <pre><code>def parse1(L):
      newL = []
      for x in L:
          if x == '}':
              return newL
          elif x == '{': 
              newL.append(parse1(L[1:]))
          else:
              newL.append(x)
      return newL
  </code></pre>
  
  <p>It works to the extent that whenever a { occurs, I reclusively pass the rest of the list into the function again, with a base case being when } occurs. This is working okay, but once it exits the recursion and creates a sublist, the element "x" it's iterating through has not gone past this section. So for example, if our list is: ['{', '1', '}'], the result should simply be [[1]]. However, what is happening is it's returning [[1],'1'], because once it creates the sublist (which seems to be working fine) the next element "x" the loop is going through is actually an element of that sublist, it's the element right after the '{', and according to my code, it's then appended to the list.</p>
  
  <p>I feel like this is a really easy fix, I have spent very long trying to figure it out though. I understand the issues, as I have explained, but cannot for the life of me figure out how to fix it. Any help would be greatly appreciated!</p>