# "Unflattening" a list

Imagine we have a list with some hierarchical structure:

list = {{{2,3},{2,4}},{{{3,4}}},{{5,6},{7,8}}};


Which we then flatten to some level in order to process the data points:

Flatten[list,1]
>> {{2, 3}, {2, 4}, {3, 4}, {5, 6}, {7, 8}}


We then do "something" to change the value of the elements in the list, without increasing or decreasing the number of elements:

>> {{5*10^9, 3}, {2, 4}, {3, 4}, {191991, 6}, {7, 8}}


Can we reverse the flattening procedure to go back to a list of the form:

>> {{{5*10^9,3},{2,4}},{{{3,4}}},{{191991,6},{7,8}}};

• Partition is essentially the inverse of Flatten Aug 14, 2013 at 0:44
• @bill s Oh, I know about Partition. My question is how we can kind of save the previous structure of the list prior to flattening, and then reapply this hierarchical structuring (which can pretty much be arbitrary).
– T.T.
Aug 14, 2013 at 0:46
• I'd presume you can't actually save the previous structure. What you might be able to do is simply map the new elements back to list, ie. replacing elements in the old list with ones in the new list in order. This should be possible since you aren't increasing or decreasing the number of elements. Aug 14, 2013 at 0:54
• @bills I feel Internal-Deflatten is also a candidate for the inverse of Flatten, and is worth mentioning :). See Szabolcs answer in the Q&A about undocumented functions. But yeah I guess it doesn't really help. Aug 17, 2013 at 21:26
• Code golf post inspired by this question: codegolf.stackexchange.com/questions/43018/unflatten-an-array Dec 31, 2014 at 18:37

### Edit

As described here, the solution below leaks symbols in the global namespace. This can be prevented by using this shorter equivalent:

unflatten[l_, o_] := Module[{i = 1, l1 = Flatten[l]},
Function[Null, l1[[i++]], {Listable}][o]
]


### Original

You can unflatten every list with the same number of elements as long as you still have your original list. When you really overwrite your original list with its flattened version, then the structure is lost forever.

Let me give an alternative approach which should work with all kinds of elements. Additionally, this function might have a nerdy touch, because it is not quite obvious why it does what it does

unflatten[l_, o_] := Module[{f, i = 1, l1 = Flatten[l]},
Attributes[f] = {Listable}; f[_] := l1[[i++]]; f[o]]


When we now take a somehow deeply structured list orig, we can restructure the list {1,2,3,4} in exactly the same way

orig = {{{}, {Exp}, 3, {{{{a}}}, c}}};
unflatten[Range, orig]

(* {{{}, {1}, 2, {{{{3}}}, 4}}} *)


OK, to give some hints: Above we want to structure the list l exactly the same way as o. The function f doesn't really do something. Every time it is called with any argument, it returns the next element from the list l by using the counter i. The important thing is, that f has the attribute Listable which makes that it is first distributed to all elements in the list, no matter how deeply they are nested. If f finally meets a list element, it returns the replacement element and the list structure is preserved. Therefore, the whole approach works because Listable functions return the same list structure as their arguments and the order in which the elements are visited is the correct one.

• @halirutan Nice.
– T.T.
Aug 14, 2013 at 3:20
• Adding to the nerdiness, can we make unflatten into a nestedRotate by setting f[_] := l1[[++i /. Length@l1 + 1 -> 1]]; or something similar? Aug 14, 2013 at 16:31
• A memory / symbol leak has been discovered for your code - which is otherwise very neat and elegant, here. Perhaps, it is a good idea to update to one of the alternatives posted there, or some other one? Apr 3, 2014 at 17:45
• @halirutan Welcome :) Actually, the minimal change would've been just using SetAttributes[f, Listable] in place of Attributes[f]={Listable}, but of course there are many ways, as usual in M. Apr 4, 2014 at 10:46
• (Just to put it here, too) The reason f leaks is that you've destroyed the Temporary attribute Apr 14, 2020 at 7:23

I faced the same problem a while ago. Here's my initial code:

copyPartition[A_, B_] := Module[{i = 0}, Map[A[[++i]]&,B,{-1}]]


It works fine most of the time:

A = {a,b,c,d,e,f,g,h,i};
B = {1, {2, 3}, {4, {5}, 6}, {7, {{8}, {9}}}};
copyPartition[A,B]
(*  {a, {b, c}, {d, {e}, f}, {g, {{h}, {i}}}} *)


But change the i in A to i$: A = {a,b,c,d,e,f,g,h,i$};
copyPartition[A,B]
(*  {a, {b, c}, {d, {e}, f}, {g, {{h}, {8}}}} *)


The i\$ got evaluated -- a situation that's not likely to occur in practice, but I still didn't like it.
My solution was to do everything twice:

copyPartishun[A_, B_] := Map[A[[#]]&, Block[{i = 0}, Map[++i&,B,{-1}]], {-1}]


That solves the problem, but it's slow, so I asked in Mathgroup.
The best answer was from J. Siehler:

partitionedAz[A_, B_] := ReplacePart[B, A, Position[B,_,{-1},Heads->False],
Transpose@{Range@Length@Flatten@B}]


### InternalCopyListStructure

Examples:

orig = {{{}, {Exp}, 3, {{{{a}}}, c}}};

InternalCopyListStructure[orig, Range]

{{{}, {1}, 2, {{{{3}}}, 4}}}

list = {{{2, 3}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}};
list1 = Flatten[list, 1];
list1[] = {2, 8};

InternalCopyListStructure[list, Flatten@list1]

 {{{2, 8}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}}


One possible solution

list = {{{2, 3}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}}


{{{2, 3}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}}

list1 = Flatten[list, 1]
list1[] = {2, 8};
list1


{{2, 3}, {2, 4}, {{3, 4}}, {5, 6}, {7, 8}}

{{2, 8}, {2, 4}, {{3, 4}}, {5, 6}, {7, 8}}

ReplacePart[list, {Sequence @@
MapThread[#1 -> #2 & , {Position[list, _?(NumberQ[#] &)] ,
Flatten[list1]}]}]


{{{2, 8}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}}

I prefer the following code. You don't bother to copy the structure; just replace the number in the original list with corresponding one.

lstOrigin = {{{2, 3}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}};
answerFlatten = Flatten@{{5*10^9, 3}, {2, 4}, {3, 4}, {191991, 6}, {7, 8}};

Block[{i = 1},

If you do this often, you may probably change the Block` into a function.