One convenient way to think of `Flatten`

with the second argument is that it performs something like `Transpose`

for ragged (irregular) lists. Here is a simple example:

```
In[63]:= Flatten[{{1,2,3},{4,5},{6,7},{8,9,10}},{{2},{1}}]
Out[63]= {{1,4,6,8},{2,5,7,9},{3,10}}
```

What happens is that elements which constituted level `1`

in the original list are now constituents at level `2`

in the result, and vice versa. This is exactly what `Transpose`

does, but done for irregular lists. Note however, that some information about positions is lost here, so we can not directly inverse the operation:

```
In[65]:= Flatten[{{1,4,6,8},{2,5,7,9},{3,10}},{{2},{1}}]
Out[65]= {{1,2,3},{4,5,10},{6,7},{8,9}}
```

To have it reversed correctly, we'd have to do something like this:

```
In[67]:= Flatten/@Flatten[{{1,4,6,8},{2,5,7,9},{3,{},{},10}},{{2},{1}}]
Out[67]= {{1,2,3},{4,5},{6,7},{8,9,10}}
```

A more interesting example is when we have deeper nesting:

```
In[68]:= Flatten[{{{1,2,3},{4,5}},{{6,7},{8,9,10}}},{{2},{1},{3}}]
Out[68]= {{{1,2,3},{6,7}},{{4,5},{8,9,10}}}
```

Here again, we can see that `Flatten`

effectively worked like (generalized) `Transpose`

, interchanging pieces at the first 2 levels. The following will be harder to understand:

```
In[69]:= Flatten[{{{1, 2, 3}, {4, 5}}, {{6, 7}, {8, 9, 10}}}, {{3}, {1}, {2}}]
Out[69]= {{{1, 4}, {6, 8}}, {{2, 5}, {7, 9}}, {{3}, {10}}}
```

The following image illustrates this generalized transpose:

We may do it in two consecutive steps:

```
In[72]:= step1 = Flatten[{{{1,2,3},{4,5}},{{6,7},{8,9,10}}},{{1},{3},{2}}]
Out[72]= {{{1,4},{2,5},{3}},{{6,8},{7,9},{10}}}
In[73]:= step2 = Flatten[step1,{{2},{1},{3}}]
Out[73]= {{{1,4},{6,8}},{{2,5},{7,9}},{{3},{10}}}
```

Since the permutation `{3,1,2}`

can be obtained as `{1,3,2}`

followed by `{2,1,3}`

. Another way to see how it works is to use numbers which indicate the position in the list structure:

```
Flatten[{{{111, 112, 113}, {121, 122}}, {{211, 212}, {221, 222, 223}}}, {{3}, {1}, {2}}]
(*
==> {{{111, 121}, {211, 221}}, {{112, 122}, {212, 222}}, {{113}, {223}}}
*)
```

From this, one can see that in the outermost list (first level), the third index (corresponding the third level of the original list) grows, in each member list (second level) the first element grows per element (corresponding to the first level of the original list), and finally in the innermost (third level) lists, the second index grows, corresponding to the second level in the original list. Generally, if the k-th element of the list passed as second element is `{n}`

, growing the k-th index in the resulting list structure corresponds to increasing the n-th index in the original structure.

Finally, one can combine several levels to effectively flatten the sub-levels, like so:

```
In[74]:= Flatten[{{{1,2,3},{4,5}},{{6,7},{8,9,10}}},{{2},{1,3}}]
Out[74]= {{1,2,3,6,7},{4,5,8,9,10}}
```

3I think it's easier to understand if the numbers indicate the original position in the nested list. For example, in the three-number permutation example, it would be

`Flatten[{{{111, 112, 113}, {121, 122}}, {{211, 212}, {{221,222,223}}}, {{3},{1},{2}}}`

and the result would read`{{{111, 121}, {211, 221}}, {{112, 122}, {212, 222}}, {{113}, {223}}}`

. – celtschk – 2012-04-13T10:18:41.533@celtschk Thanks, but I am not convinced. For me personally, it is easier to track visually distinct numbers and see where they end up in the new structure. But feel free to add this to my answer, this is perfectly fine with me. – Leonid Shifrin – 2012-04-13T10:55:15.017

I guess our ways of understanding are just different in that respect. Actually only after rewriting my way, I completely understood the cyclic permutation (but then directly, without the two-step decomposition you did afterward). The point is that this way, you can see immediately which index changes if you move along each list, and you can determine where the number originated in the list structure from without even looking at the original list. In other words, it more directly (at least to me) reveals the

structureof the transformation. – celtschk – 2012-04-14T18:39:08.047@celtschk Yes, I understood your reasoning. But for me, it was easier to understand which elements jumped which lists, rather than look at elements' indices. May be it's just me, I always gravitated to geometry rather than algebra. I think both ways have their merits. – Leonid Shifrin – 2012-04-14T19:05:48.437

OK, I've now inserted the example using index-numbers with corresponding explanation. – celtschk – 2012-04-14T19:14:13.993

Hmmm ... maybe then an image would make it easier to understand (basically draw the original and final list structure with boxes, and use arrows to show where the elements go). – celtschk – 2012-04-14T19:16:22.120

@celtschk Perhaps, but it may be hard to do for levels deeper than 2, also because the (sub)lists themselves change. Some kind of diagrams similar to those commonly used to visualize permutations, may indeed be useful. Thanks for your addition, b.t.w. – Leonid Shifrin – 2012-04-14T19:22:28.040

I've now drawn an image of the transformation and added it. I think it indeed helps in understanding the transformation. – celtschk – 2012-04-14T20:10:59.010

@celtschk Thanks, this is really useful! It is now quite clear what is going on, by looking at it. I should've done that. – Leonid Shifrin – 2012-04-14T20:14:01.253

1@LeonidShifrin For

`In[63]:= Flatten[{{1,2,3},{4,5},{6,7},{8,9,10}},{{2},{1}}] Out[63]= {{1,4,6,8},{2,5,7,9},{3,10}}`

,you say What happens is that elements which constituted level 1 in the original list are now constituents at level 2 in the result. I don't quite understand, input and output have the same level structure, elements are still in the same level.Could you explain this at large? – withparadox2 – 2012-08-31T05:02:16.330