Time efficient Partial Inverted Index building
I need to build a partial Inverted Index . Something like: l = {{x, {h, a, b, c}}, {y, {c, d, e}}} iI[l] (* -> {{a, {x}}, {b, {x}}, {c, {x, y}}, {d, {y}}, {e, {y}}, {h, {x}}} *) I think it is pretty clear what it does. In the input list, the {x, y ...} are unique, while the {a, b, c, ..} are not. The output ought to be ordered by #[[1]] . Right now, I am doing this: iI[list_List] := {#, list[[Position[list, #][[All, 1]]]][[All, 1]]} & /@ (Union@Flatten@Last@Transpose@list) But it looks too convoluted for such an easy task, seems too slow, and I should be able to cope with Legion. A test drive