title: Translate Tcl List to Python List
body: <p>I try to translate Tcl list to python list. </p>
  
  <p>There are two problems: </p>
  
  <ul>
  <li>If a list in the original list contains only a single list, the translation is incorrect. For example, <code>{{12 34}}</code> is not correctly translated.</li>
  <li>The option to convert all numbers to a type does not work.</li>
  </ul>
  
  <p>Python 3 code:</p>
  
  <pre><code>import tkinter
  
  
  class TclInterpreter(object):
      def __init__(self):
          self._tcl = tkinter.Tcl()
  
      def eval(self, tcl_cmd):
          return self._tcl.eval(tcl_cmd)
  
  
  class TclPyListTranslator(object):
      def __init__(self, tcl):
          self._tcl = tcl
  
      def to_py(self, tcl_list, dtype=str):
          # convert a Tcl List to python list, also convert elements of each leaf
          # node to dtype
          self._tcl.eval("set tcl_list %s" % tcl_list)
          numItems = int(self._tcl.eval("llength $tcl_list"))
          if numItems &gt; 1:
              result = [self._tcl.eval("lindex $tcl_list %d" % i) for i in range(
                  numItems)]
              for i in range(numItems):
                  result[i] = self.to_py("{" + result[i] + "}", dtype)
          else:
              result = dtype(self._tcl.eval("lindex $tcl_list %d" % 0))
          return result
  
  
  inter = TclInterpreter()
  translator = TclPyListTranslator(inter)
  tcl_list = "{12 {{12 34}} {56 {78 {11 12} 10}}}"
  
  # prints ['12', '12 34', ['56', ['78', ['11', '12'], '10']]]
  # The '12 34' is incorrect
  print(translator.to_py(tcl_list))
  
  # does not run
  print(translator.to_py(tcl_list, int))
  </code></pre>