Search code examples
python-3.xlisttuplesparenthesesbrackets

redefining list by changing brackets for parentheses and parentheses for brackets


How may I convert the square brackets inside a list into parentheses and the parentheses outside the square brackets into square brackets?

Example:

Lst=[([(1,2)],[(1,2),(1,3)]),([(1,4)],[(1,4),(2,3)])]

What I would ideally like to have is:

Lst=[[((1,2)),((1,2),(1,3))],[((1,4)),((1,4),(2,3))]]

because as you may notice, the parentheses in the original list encloses two pairs of brackets, but I want it the other way around, for the brackets to enclose two pairs of parentheses.


Solution

  • You are trying to convert innerlist to tuple and outer tuple back to list. You can do this by list comprehension.

    In [1]: Lst=[([(1,2)],[(1,2),(1,3)]),([(1,4)],[(1,4),(2,3)])]
    
    In [2]: [list(tuple(k) for k in x) for x in (y for y in Lst)]
    Out[2]: [[((1, 2),), ((1, 2), (1, 3))], [((1, 4),), ((1, 4), (2, 3))]]