Simple python string to list parser So I came across this question on SO and I felt like it would be a cool thing to try and write a parser for since I always wanted to try it. So I present to you:
My first time writing a parser.
It converts strings like this:
&quot;a,s,[c,f],[f,t], [[a,c],[d3,32]]&quot;

into list objects
['a', 's', ['c', 'f'], ['f', 't'], [['a', 'c'], ['d3', '32']]]

Here is my code for now
def parseToList(string, cont=0):
  result = list()
  temp = ''
  i = cont
  while i &lt; len(string):
    if string[i] == ',':
      if len(temp) and temp != ' ':
        result.append(temp)
      temp = ''
    elif string[i] == '[':
      res = parseToList(string, i+1)
      i = res[1]
      result.append(res[0])
    elif string[i] == ']':
      if len(temp) and temp != ' ':
        result.append(temp)
      return (result,i)
    else:
      temp += string[i]  
    i += 1
  if len(temp) and temp != ' ':
    result.append(temp)
  return (result, i)

def listParse(string):
  return parseToList(string)[0]

s = 'a,s,[c,f],[f,t], [[a,c],[d3,32]]'

print(s)
print(listParse(s))

Is there anything I am doing wrong? Something I should change?
Here are a few things that came to my mind:

Bug

if temp != ' ' will not work when there are more than 1 consecutive spaces.
To fix this, use if not temp.isspace() instead of comparing with a hard-coded string.
For example, s = 'a,   [b]' will output ['a', ['b'], '   '] for your current code.

Your code outputs ['a', ' b'] for a, b. I'll assume that including the space is a feature and not a bug.



Design

Wrap the test code inside  if __name__ == '__main__'. This will prevent the code from being called when being imported from another module.

Function names should preferably be lowercase. Change the CamelCase names to snake_case.

In return statements, you need not enclose the items in a parenthesis if you are returning a tuple

result = list() can be replaced with just result = []

if len(temp) can be replaced with just if temp. The bool of empty values are False in python.


res = parse_to_list(string, i + 1)
i = res[1]
result.append(res[0])

The above can be a bit simplified and can be made a bit more understandable.
nested_list, i = parse_to_list(string, i + 1)
result.append(nested_list)


Instead of using string[i], you can declare a new element char which is equal to string[i]
(This is just my personal preference)

You can declare parse_to_list to inside list_parse. This will remove the need to pass string inside a recursion repeatedly, and will also make the inner function &quot;private&quot;.
(But this is also just my personal preference)


The final code should look something like this after applying the above:
def list_parse(string):
    def parse_to_list(cont=0):
        result = []
        temp = ''
        i = cont

        while i &lt; len(string):
            char = string[i]

            if char == ',':
                if temp and not temp.isspace():
                    result.append(temp)
                temp = ''

            elif char == '[':
                nested_list, i = parse_to_list(i + 1)
                result.append(nested_list)

            elif char == ']':
                if temp and not temp.isspace():
                    result.append(temp)
                return result, i

            else:
                temp += char

            i += 1

        if temp and not temp.isspace():
            result.append(temp)

        return result, i

    return parse_to_list()[0]


if __name__ == '__main__':
    s = 'a,s,[c,f],[f,t], [[a,c],[d3,32]]'

    print(list_parse(s))