Converting an input string to a list of sub lists with numbers I am learning Python 3. One thing I am doing to get some repetition is going through checkio. It is a website with coding exercises. One of the exercises gives an input string as follows:

str_to_nbrs("(2,2),(6,2),(2,6)")

str_to_nbrs("(3,7),(6,10),(9,7)")


I need the output of this code to produce a list of sub lists with the pairs of numbers so I can put them into variables to use for calculations. 

This exercise actually has to do with x and y coordinates and circles. I am not sure why this input was in the form of a string. But, it gives me a chance to learn how to use the tools to convert a string like this to numbers that I can use in mathematical processes.

I figured out how to accomplish what I need. I am relatively new to Python 3. But, from what I have seen already, it is a well-thought-out and efficient language. I would bet there is a better way to do it with less code. 

Please note that I added the big obvious comments to explain what the code is doing. They are not part of my regular coding style.

One more thing: I am still trying to come up with a convention for variable names. I have started lists with "l_". I try to give things meaningful names, but also try to keep them relatively short. I know "l_work" isn't a good name. But, I just wanted to finish that section of code, and used that...err...unimaginative name.

import re

# str_to_nbrs("(2,2),(6,2),(2,6)")

def str_to_nbrs(data):
    print("String input:")
    print(data)
    print()
    new_data = re.sub(r"\(","",data)
    new_data = new_data + ","

    print("Remove left parenthesis and add a comma a the end for split method:")
    print(new_data) 
    print()

    l_data = new_data.split("),")
    l_data.pop()
    print("Reformatted string data in a list:")
    print(l_data)
    print()

    l_work = []
    l_nbrs = []
    l_all = []

    for i in l_data:
        l_work = i.split(",")
        for j in l_work:
            l_nbrs.append(int(j))
        l_all.append(l_nbrs)
        l_nbrs = []
    print("Integers in sub lists:")
    print(l_all)
    print()
    print("---------------------")
    print()


str_to_nbrs("(2,2),(6,2),(2,6)")
str_to_nbrs("(3,7),(6,10),(9,7)")
re.sub(r"\(","",data) is not a very good use of regular expressions. You could have more easily achieved the same result with data.replace("(", "").

But since you are already using regular expressions, why not use them to do more:

&gt;&gt;&gt; re.findall(r'\d+,\d+', "(2,2),(6,2),(2,6)")
['2,2', '6,2', '2,6']


This regular expression finds all digits (\d) (allows also multiple digits using +), which are separated only by a comma. Since different pairs also have parenthesis around them, this just gives you a list of the pairs.

By adding parenthesis around the digits, this captures each of them and splits each pair:

&gt;&gt;&gt; re.findall(r'(\d+),(\d+)', "(2,2),(6,2),(2,6)")
[('2', '2'), ('6', '2'), ('2', '6')]


Now all that is left to do is convert them to integers and put it in a function:

def str_to_nbrs(data):
    return [list(map(int, p)) for p in re.findall(r'(\d+),(\d+)', data)]