6
$\begingroup$

I have a list that looks like:

z={{{a,b},{b,c},{c,d},{d,e}},{{b,c},{d,e},{w,e}}}

How would I create a list that looks like:

zz={{a,b,b,c,c,d,d,e},{b,c,d,e,w,e}}

I have tried Flatten but that doesn't seem to work at the desired level.

$\endgroup$
  • 1
    $\begingroup$ Try mapping Flatten $\endgroup$ – M.R. Jun 2 at 3:39
  • $\begingroup$ Yeah that's exactly what i was missing, thanks! $\endgroup$ – D'Angelo Jun 2 at 3:43
10
$\begingroup$

You can tell Flatten to leave level 1 as-is but to flatten levels 2 and 3 together:

Flatten[z, {{1}, {2, 3}}]
(* {{a, b, b, c, c, d, d, e}, {b, c, d, e, w, e}} *)

This syntax is quite flexible, see Flatten command: matrix as second argument.

| improve this answer | |
$\endgroup$
8
$\begingroup$

Try the map function

list = {{{a, b}, {b, c}, {c, d}, {d, e}}, {{b, c}, {d, e}, {w, e}}}
Flatten /@ list
| improve this answer | |
$\endgroup$
  • $\begingroup$ Yes that was it, thank you. $\endgroup$ – D'Angelo Jun 2 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.