# transforming lists to a list of lists

How can I convert the following lists to a list of lists?

{
Flatten[{Range[2, 4, 1]}],    Flatten[{Range[6, 20, 1]}],
Flatten[{Range[26, 28, 1]}], Range[30, 31, 1], Range[35, 36, 1],
Table[
{
Flatten[{Range[2 + n, 4 + n, 1]}],
Flatten[{Range[6 + n, 20 + n, 1]}],
Flatten[{Range[26 + n, 28 + n, 1]}], Range[30 + n, 31 + n, 1],
Range[35 + n, 36 + n, 1]
}, {n, 36, 2304, 36}
]
}


I want to transform the output generated by the above Code to something like, for example,

{{1,2,5}, {6,9,67,56},...,{4,45,67,5,7,7}}


I just want to get rid of unnecessary curly brackets generated.

• See this answer Commented Jan 27, 2021 at 1:55
• Can you explain the logic of the ranges? Perhaps there is a more elegant way to generate this, maybe by generating a full range and then eliminating unwanted entries? Commented Jan 27, 2021 at 2:11

Flatten[# + Range[{2, 6, 26, 30, 35}, {4, 20, 28, 31, 36}] & /@ Range[0, 2304, 36], 1]


You can also use

Flatten[# + (Range @@@ {{2, 4}, {6, 20}, {26, 28}, {30, 31}, {35, 36}}) & /@
Range[0, 2304, 36], 1]

% == %%

True


This does what you want, with fewer Flatten (Note that Flatten[{Range[...]}] is the same as Range[...]):

Table[
{Range[2 + n, 4 + n, 1],
Range[6 + n, 20 + n, 1],
Range[26 + n, 28 + n, 1],
Range[30 + n, 31 + n, 1],
Range[35 + n, 36 + n, 1]},
{n, {0} ~ Join ~ Range[36, 2304, 36]}
] ~ Flatten ~ 1


The first part of your lists, generated "by hand" in your code with the explicit Range calls, would have been generated by your Table expression for $$n=0$$, so I explicitly added that value of $$n$$ to the Table iterator, in addition to the other values that were already included there.

• I wanted to create an index of numbers for aggregation of the selected rows and columns in a matrix. For example, {2,3,4} in the list of lists is used to sum up the elements in the associated rows and columns and maintains the resulting row/column as a new matrix element, while dropping the individual rows/columns summed up. This way matrix dimension is reduced by aggregation. Basically, I wanted to aggregate the selected rows/columns of a matrix. Thanks for your answer. Commented Jan 27, 2021 at 12:42

I found out that the following does what I want, but I do not like it at all because there are many Flatten commends around. It looks ugly to me. Maybe someone can improve upon this.

Flatten[
{
{
Flatten[{Range[2, 4, 1]}],    Flatten[{Range[6, 20,  1]}],
Flatten[{Range[26, 28, 1]}], Range[30, 31, 1], Range[35, 36, 1]
},
Flatten[
Table[
{
Flatten[{Range[2 + n, 4 + n, 1]}],
Flatten[{Range[6 + n, 20 + n, 1]}],
Flatten[{Range[26 + n, 28 + n, 1]}], Range[30 + n, 31 + n, 1],
Range[35 + n, 36 + n, 1]
}, {n, 36, 2304, 36}
], 1]
}, 1]