# Transforming a list of the form {{{x, y}, z}, ... } into the form {{x, y, z}, ... } [duplicate]

This should be a fairly simple question, but I can't figure out how to do it. I have a list of points of the following form:

{ {{x1, y1}, z1}, {{x2, y2}, z2} ... }


This form is useful for creating an interpolation function. However, if I want to plot a list contour plot of the same data, I need an array of the form:

{ {x1, y1, z1}, {x2, y2, z2} ... }


Is there an easy way to convert between the two, without generating the data again from scratch in the new format? I tried a few things with Flatten[] but that doesn't seem to work for me.

• Flatten /@ list or Append @@@ list should work. Jun 21 '16 at 17:15
• It worked. Thanks! How is Flatten/@list different from Flatten[list,1]? Jun 21 '16 at 17:17
• Flatten[list,1] basically says "get rid of all the curly braces at level 1". So, if you have a list like {{{1, 2}, 3}, {{4, 2}, 1}}, the elements at level 1 are {{1, 2}, 3} and {{4, 2}, 1}}; remove the outermost braces on each of those, and splice them back into the list, you get {{1, 2}, 3, {4, 2}, 1}. Jun 21 '16 at 17:22
• @J.M. Upvote for that Append. :)
– yode
Jun 21 '16 at 17:32
• Before this gets closed as a "Simple mistake", there's bound to be a duplicate somewhere it should point to instead Jun 21 '16 at 18:44

data = {{{x1, y1}, z1}, {{x2, y2}, z2}, {{x3, y3}, z3}, {{x4, y4}, z4}};


J.M.

Flatten /@ data
Append @@@ data


m_goldberg

ArrayReshape[data, {Length[data], 3}]
Block[{h}, h[{{a_, b_}, c_}] := {a, b, c}; h /@ data]


All of the above return

{{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}, {x4, y4, z4}}

♭ = ## & @@@ {##} & @@@ # &;

♭ @ {{{x1, y1}, z1}, {{x2, y2}, z2}}


{{x1, y1, z1}, {x2, y2, z2}}

• This looks "exotic" )) +1 Jun 21 '16 at 19:39

another option is Cases

lst = {{{x1, y1}, z1}, {{x2, y2}, z2}}
Cases[lst, {{x__}, y__} :> {x, y}]


## Example

Code

Partition[Flatten @ data , 3]


Output

{{x1, y1, z1}, {x2, y2, z2}}

Note: data is your original list

Reference

• Could be dangerous if any of the elements are lists themselves. Jun 21 '16 at 19:56
• @ChipHurst I agree, but OP does not indicate such case or a need for treatment of such cases ;s Jun 21 '16 at 19:58

There are many answers, but I want to give another one that could be not so elegant, but is very suitable for easy modification. It's quite often that you have a list of "objects", where object can be a weirdly nested list, and you need to extract some subset of components possible in different order.

l = {{{x1, y1}, z1}, {{x2, y2}, z2}};
{#[[1, 1]], #[[1, 2]], #[[2]]} & /@ l


Here it's really obvious what's happening and if you need different components, you can easily modify indices.