traversing string from the end .
n = len ( string ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( string [ i ] == " [ " ) : NEW_LINE INDENT s = list ( ) NEW_LINE while ( arr [ - 1 ] != " ] " ) : NEW_LINE INDENT s . append ( arr [ - 1 ] ) NEW_LINE arr . pop ( ) NEW_LINE DEDENT arr . pop ( ) NEW_LINE DEDENT DEDENT