How to recursively remove the first set of parantheses in string of nested parantheses? (in Python)
<p>EDIT: 
Say I have a string of nested parentheses as follows: ((AB)CD(E(FG)HI((J(K))L))) (assume the parantheses are balanced and enclose dproperly
How do i recursively remove the first set of fully ENCLOSED parantheses of every subset of fully enclosed parentheses?</p>

<p>So in this case would be (ABCD(E(FG)HI(JK)). (AB) would become AB because (AB) is the first set of closed parentheses in a set of closed parentheses (from (AB) to K)), E is also the first element of a set of parentheses but since it doesn't have parentheses nothing is changed, and (J) is the first element in the set ((J)K) and therefore the parentheses would be removed.</p>

<p>This is similar to building an expression tree and so far I have parsed it into a nested list and thought I can recursively check if the first element of every nested list isinstance(type(list)) but I don't know how?</p>

<p>The nested list is as follows:</p>

<pre><code>arr = [['A', 'B'], 'C', 'D', ['E', ['F', 'G'], 'H', 'I', [['J'], 'K']]]
</code></pre>

<p>Perhaps convert it into:</p>

<pre><code>arr = [A, B, C, D, [E, [F, G], H, I, [J, K]]
</code></pre>

<p>Is there a better way?</p>

<p>You need to reduce your logic to something clear enough to program.  What I'm getting from your explanations would look like the code below.  Note that I haven't dealt with edge cases: you'll need to check for None elements, hitting the end of the list, etc.</p>

<pre><code>def simplfy(parse_list):

    # Find the first included list; 
    #   build a new list with that set of brackets removed.
    reduce_list = []
    for pos in len(parse_list):
        elem = parse_list[pos]
        if isinstance(elem, list):
            # remove brackets; construct new list
            reduce_list = parse_list[:pos]
            reduce_list.extend(elem)
            reduce_list.extend(parse_list[pos+1:]

    # Recur on each list element
    return_list = []
    for elem in parse_list
        if isinstance(elem, list):
            return_list.append(simplfy(elem))
        else:
            return_list.append(elem)

    return return_list
</code></pre>

python string algorithm recursion