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$
2
  • 1
    $\begingroup$ Try mapping Flatten $\endgroup$
    – M.R.
    Jun 2, 2020 at 3:39
  • $\begingroup$ Yeah that's exactly what i was missing, thanks! $\endgroup$
    – D'Angelo
    Jun 2, 2020 at 3:43

2 Answers 2

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.

$\endgroup$
8
$\begingroup$

Try the map function

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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