# List of (sub-)lists - query sub-lists by names?

Given the following very simple example:

list1 = {a11, a12, a13, a14, a15};
list2 = {a21, a22, a23, a24, a25};
list3 = {a31, a32, a33, a34, a35};
biglist = {list1, list2, list3};


are the names "list1", "list2", "list3" of the sub-lists "lost" inside the "biglist"? For example, if I need to know only which sub-lists are included in the "biglist" (by name), is this possible? Further, can I get, for example, the second element of all sub-lists, can I use something like newlista = biglist[[list3, [[2]]]] instead of newlist = biglist[[3,2]] ? This would be useful, for example, in case I don't know the order of the sub-lists inside the "biglist".

• What version do you use? Associations are the way to go if 10+.
– Kuba
Feb 22, 2017 at 15:07
• I have version 11, so it should be available. Thanks, I'll test it right now! It's difficult for me to search for functions which were new added to Mathematica: there are hundreds of them :-). Feb 22, 2017 at 15:12
– Kuba
Feb 22, 2017 at 15:14
• Yes, in a way... Feb 22, 2017 at 16:07
• oh, then I was thinking about something like biglist = <|"list1" -> list1,...|>
– Kuba
Feb 22, 2017 at 16:44

Elucidating Kuba's comments, here's how you can use Association to accomplish your goals.

list1 = {a11, a12, a13, a14, a15};
list2 = {a21, a22, a23, a24, a25};
list3 = {a31, a32, a33, a34, a35};

lists = <|"list1" -> list1, "list2" -> list2, "list3" -> list3|>;


(After lists is defined list1 etc. may be cleared.)

### I need to know only which sub-lists are included in the "biglist" (by name)

Keys[lists]

{"list1", "list2", "list3"}


### can I get, for example, the second element of all sub-lists

lists[[All, 2]] // Values

{a12, a22, a32}


### can I use something like newlista = biglist[[list3, [[2]]]] ?

lists[["list3", 4]]

a34


### for example, in case I don't know the order of the sub-lists inside the "biglist"

(* pick one *)

{"list2", "list1"} /. lists

lists /@ {"list2", "list1"}

Lookup[lists, {"list2", "list1"}]

{{a21, a22, a23, a24, a25}, {a11, a12, a13, a14, a15}}

• Thanks a lot for your very precise and detailed answer! Feb 23, 2017 at 6:42
• Works perfect, it's a very logical way to do it and it's exactly what I was looking for! Feb 23, 2017 at 6:51

I made a simple functions which - I think - could help:

l1 = {"a", "b", "c", "d"};
l2 = {"e", "f", "g"};
l3 = {"h", "i", "j", "k", "l"};
l = {l1, l2, l3};

In[40]:= l[[3, 2]]

Out[40]= "i"

In[50]:= picker[a_?ListQ, x_?ListQ, y_?NumberQ] :=
If[SubsetQ[Flatten[a], x], x[[y]]]

In[52]:= picker[l, l3, 2]

Out[52]= i

• Also a useful approach, thanks. Feb 23, 2017 at 6:43