Display lists in a table form

I would like to display the lists in a table as in the image.
How can I make it work?

list1 = {1/x, -1 + x, -(x/(-2 + x)), -1 + 1/x, -1 + 2 x};
list2 = {{{a, b}}, {{c, d}}, {{a, c}, {b, d}}, {{e, f}, {g, h}}, {{k,
t}}};
Grid[{list1, list2}, Frame -> All]


UPDATE:
I would like to add some more information. list1 and list2 are data from some other processing so I have it in advance.
The obove lists are just for example. The real lists are much longer and it would be nice to have able to do it automatically. list1 and list2 have the same size. The length can be change and each element in list2 may have one, two or more many sublists.

• Have you considered formatting it into a Dataset? Commented Apr 11, 2021 at 23:24
• @CATrevillian I didn't know that it exists. Commented Apr 11, 2021 at 23:28

Lot's of ways to achieve this. A few follow:

list1 = {"list1", 1/x, -1 + x, -(x/(-2 + x)), -1 + 1/x, -1 + 2 x};
list2 = {"list2", {{a, b}}, {{c, d}}, {{a, c}, {b, d}}, {{e, f}, {g, h}}, {{k, t}}};
list3 = {"No.", 1, 2, 3, 4, 5};

Grid[Transpose[{list3, list2, list1}], Frame -> All]


This gets a bit closer:

list1 = {"list1", 1/x, -1 + x, -(x/(-2 + x)), "", -1 + 1/x,
"", -1 + 2 x};
list2a = {"", 1, 2, 3, 4, 5, 6, 7};
list2 = {"list2", {a, b}, {c, d}, {a, c}, {b, d}, {e, f}, {g, h}, {k,
t}};
list3 = {"No.", 1, 2, 3, SpanFromAbove, 4, SpanFromAbove, 5};

Grid[Transpose[{list3, list2a, list2, list1}], Frame -> All,
Spacings -> 2]


Even more close to your drawing:

Grid[Transpose[{list3, list2a, list2, list1}],
Spacings -> 2,
Dividers -> {
{1 -> True, 2 -> True, 4 -> True, 5 -> True} (*Vertical*),
{1 -> True, 2 -> True, 3 -> True, 4 -> True, 6 -> True, 8 -> True,
9 -> True}(*Horizontal*)
}]


Your approach will depend on "all" of what you want to do, e.g.:

• A one-off table?
• An interface (maybe using Manipulate) to generate such displays dynamically?

In response to the OPs update of the question:

list1 = {1/x, -1 + x, -(x/(-2 + x)), -1 + 1/x, -1 + 2 x};
list2 = {{{a, b}}, {{c, d}}, {{a, c}, {b, d}}, {{e, f}, {g, h}}, {{k,
t}}};

list2elementCount = Range[Length[Flatten[list2, 1]]];
elementCounts = Length[#] & /@ list2;
list2elementNumbers =
Column[#] & /@ TakeList[list2elementCount, elementCounts];
list2Column = Column[#] & /@ list2;

Grid[Prepend[
Transpose[{mainRows, list2elementNumbers, list2Column,
list1}], {"No.", "", "list2", "list1"}],
Spacings -> {1, 1},
Alignment -> {Center, Top},
Dividers -> All]


The should work in all contexts presented by the OP.

• This is nice but you kind of manually set Dividers and list3 part. In my case, the number of elements are much bigger and this would take long time. Commented Apr 11, 2021 at 16:45
• @anhnha -- I recommend that you add to your question. Make the question more specific. If you can articulate the conditions, under which you want your "list 2" elements grouped, one can readily introduce them as conditionals in setting Dividers. A description or explanation of what you need to do and its context will help you get better answers. Otherwise everyone helping has to try and read your mind/intention. Commented Apr 11, 2021 at 16:56
• As example what do the values in the two lists represent? Additionally, do you structure the data or do you get it from somewhere? What you do with the structure of the data (e.g., the hierarchy of lists) and when in your process, will do much to simplify or complicate the solution. Also, at lease in the example given, one can fix the vertical dividers, the top row dividers, and the top, bottom, and left and right side dividers. As such, introducing conditional formatting should not take too much work. It seems like you only need to do so for the horizontal dividers. Commented Apr 11, 2021 at 16:59
• You're right. Your answer is pretty good for the given question. I'll add some more details so there may be some modifications to make it work for a large data. Commented Apr 11, 2021 at 17:17
• @anhnha -- You can just change Alignment -> {Center, Top} to Alignment -> Center for the alignment you want to try. I recommend you review Grid in the Wolfram Documentation, it will give you lots of ideas for formatting. Also, feel free to post your answer to your question. Great way to receive comments. Commented Apr 11, 2021 at 23:03

My code is long and sometimes redundant. I'd like post it here to learn how to fix that.

list1a = {1/x, -1 + x, -(x/(-2 + x)), "", -1 + 1/x, "", -1 + 2 x};
list1b = Join[{ Style["list1", 16, Bold]}, list1a]
(* tuples*)
list2a = {{{a, b}}, {{c, d}}, {{a, c}, {b, d}}, {{e, f}, {g, h}}, {{d,
t}}};
list2b = Join[{Style["list2", 16, Bold]}, Flatten[list2a, 1]];
(*span for col1*)
span = Length[#] & /@ list2a;
tupNum = {Style["Tup No.", 16, Bold], Sequence @@ Range[Plus @@ span]};
(*replace span position with -1*)
spanList = Flatten[If[# > 1, ConstantArray[-1, # - 1], 0] & /@ span]
spanPos = Position[spanList, _?(# != 0 &)] + 1;
spancol1 =
Join[{Style["Set No.", 16, Bold]},
Insert[Range[Length[list2a]], SpanFromAbove, spanPos]]
(*horizontal dividers*)
horizDiv =
Thread[Join[{1, 2}, Accumulate[Length[#] & /@ list2a] + 2] ->
ConstantArray[True, Length[list2a] + 2]];
Grid[Transpose[{spancol1, tupNum, list2b, list1b}], Spacings -> 2,
Dividers -> {{1 -> True, 2 -> True, 3 -> True, 4 -> True,
5 -> True} (*Vertical*), horizDiv(*Horizontal*)},
Background -> {{1 -> GrayLevel[.95], 2 -> LightBlue, 3 -> LightGreen,
4 -> LightOrange}, None}]