2
$\begingroup$

Is there a way to flatten the sub-lists within a list?

Transform

{{{1, x}}, {{2, x}}, {{3, x}}, {{4, x}}, {{5, x}}, {{6, x}}, {{7, x}}, {{8, x}}, {{9, x}}, {{10, x}}}

Into

{{1, x}, {2, x}, {3, x}, {4, x}, {5, x}, {6, x}, {7, x}, {8, x}, {9, x}, {10, x}}

I know I can do

Flatten /@ {{{1, x}}, {{2, x}}, {{3, x}}, {{4, x}}, {{5, x}}, {{6, x}}, {{7, x}}, {{8, x}}, {{9, x}}, {{10, x}}}

I am wondering whether there are some dedicated functions for such sub-list flattening?

$\endgroup$
  • 3
    $\begingroup$ try Flatten[list,1] $\endgroup$ – J42161217 Apr 25 '19 at 0:55
  • 1
    $\begingroup$ One can also consider using Join @@ yourlist. $\endgroup$ – Αλέξανδρος Ζεγγ Apr 25 '19 at 3:27
7
$\begingroup$

You are doing redundant step with /@, Flatten can make this directly:

Flatten[{{{1, x}}, {{2, x}}, {{3, x}}, {{4, x}}, {{5, 
    x}}, {{6, x}}, {{7, x}}, {{8, x}}, {{9, x}}, {{10, x}}}, 1]
(* {{1, x}, {2, x}, {3, x}, {4, x}, {5, x}, {6, x}, {7, x}, {8, 
  x}, {9, x}, {10, x}} *)
| improve this answer | |
$\endgroup$
1
$\begingroup$

Yes, there is a built-in function for what you want to do.

data = {{{1, x}}, {{2, x}}, {{3, x}}, {{4, x}}, {{5, x}}, {{6, x}}, {{7, x}}, {{8, x}}, {{9, x}}, {{10, x}}};
Catenate @ data
{{1, x}, {2, x}, {3, x}, {4, x}, {5, x}, {6, x}, {7, x}, {8, x}, {9, x}, {10, x}}
| improve this answer | |
$\endgroup$

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