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$

closed as off-topic by MarcoB, m_goldberg, Henrik Schumacher, happy fish, bbgodfrey Apr 26 at 20:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, m_goldberg, Henrik Schumacher, happy fish, bbgodfrey
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ try Flatten[list,1] $\endgroup$ – J42161217 Apr 25 at 0:55
  • 1
    $\begingroup$ One can also consider using Join @@ yourlist. $\endgroup$ – Αλέξανδρος Ζεγγ Apr 25 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}} *)
$\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}}
$\endgroup$

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