1
$\begingroup$

Say I have several lists L1, L2,L3 an I want to correspond to each of the elements of Union[L1,L2,L3] a list of list names to which it belongs.

How to do it efficiently? I know it is silly question but to save time I decide just to ask it.

$\endgroup$

1 Answer 1

2
$\begingroup$
lists = {L1, L2, L3} = {{1, 2, 3}, {2, 3, 4}, {1, 2, 3, 4, 5}};
labels = {"L1", "L2", "L3"};

Merge[Identity][AssociationThread @@@ Thread[{lists, labels}]]

<|1 -> {"L1", "L3"}, 2 -> {"L1", "L2", "L3"}, 3 -> {"L1", "L2", "L3"}, 4 -> {"L2", "L3"}, 5 -> {"L3"}|>

$\endgroup$
1
  • $\begingroup$ Perfect one! Thank you again you saved me twice already, I owe you! $\endgroup$ Commented Sep 9, 2019 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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