Question: I have a question that is very similar but not exactly the same as [this](https://stackoverflow.com/questions/6309472/matplotlib-can-i-create-axessubplot-objects-then-add-them-to-a-figure-instance) and [this](https://stackoverflow.com/questions/56632987/matplotlib-figure-figure-add-subplots-and-add-axes-return-none-instead-of-ax) questions.

I have 2 `pyplot.subplots()` generating `fig` and `ax` objects, and I would like to add the last one to the last position of the first `fig`.

```
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({'a' :['one','one','two','two','one','two','one','one','one','two'], 'b': [1,2,1,2,1,2,1,2,1,1], 'c': [1,2,3,4,6,1,2,3,4,6]})

fig, ax = plt.subplots(ncols=3)
ax=ax.flatten()

for i in [0,1,2]:
    if(i<2):
        myvar=["b","c"][i]
        sns.boxplot( data=df,
            x='a', y=myvar, ax=ax[i])

### some other fig
fig2,ax2 = plt.subplots(figsize=(4,5))
g=sns.scatterplot(data=df, x='b', y='c')

### I have tried the following, but both of which return only the last figure generated
fig.axes.append(ax2) #
# or:
ax[2]=ax2

# I have also tried the following, but this returns an error:
#fig.add_axes(ax2)

plt.show() 

```

Edit: Further, would it be possible to do this appending should the last figure have multiple rows or columns?

Answer: 
Just walk the string one character at a time from left to right (ranged-for is good for this), and keep track of the nesting level (number of unmatched `[`).

Rule 1: obvious test on the character

Rule 2: if `[` increment the nesting level, if `]` decrement the nesting level. If the level becomes negative that's an error. If it isn't back to zero at the end of the string that's an error.

Rule 3: if `;` then the nesting level must be at least 1, if it is (less than or) equal to zero that is an error.
Well this idea is like @Jasmeet but with all conditions just like @BenVoidt says:

```
bool check(string x){
    int bracketSence=0;
    for(int i=0; i < x.size(); ++i){
        //First Condition
        if(islower(x[i]) || isupper(x[i]) || (x[i] >= '0' && x[i]<= '9') ||
           x[i] == '[' || x[i] == ']' || x[i] == ';'){
               //Second and Third Condition
               if(x[i] == '[')      ++bracketSence;
               else if(x[i] == ']') --bracketSence;
               else if(x[i] == ';' && bracketSence<1) return false;
               if(bracketSence < 0) return false;
           }
        else return false;
    }
    return bracketSence == 0;
}

```