# How to find a specific element of a nested list

Consider the following list:

{{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}}


I want to obtain the second element of a sub-list with respect to the first element of the sub-list.

for example: given 0.4 I would like to retrieve 6, given 0.1 I would like to retrieve 5.

The first element in the sub-lists are monotonically increasing and non repetitive.

• Have you tried anything?
– Kuba
Commented May 16, 2018 at 13:55
• @Kuba, now there are many things to try and to learn from. best Commented May 16, 2018 at 15:17

Two possible examples (say for example you are interested in 0.1, but it works with any entry):

list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};
Cases[list, {0.1, _}]
Select[list, #[[1]] == 0.1 &]


Edit: if you only want the second element, go for:

Cases[list, {0.1, _}][[1, 2]]
Select[list, #[[1]] == 0.1 &][[1, 2]]

• Consider also Cases[list, {0.1, b_} -> b] to return only the second element of each sublist. Commented May 16, 2018 at 14:05

Another approach is to convert your List to an Association:

list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};
arse[0.4] (* 6 *)
arse[0.1] (* 5 *)


This assumes that each Key (each first element of a pair) are unique, but otherwise your question would be ill-posed.

• Please, don't call associations that way... ;) Commented May 16, 2018 at 15:21
• @HenrikSchumacher Aah, I was going to ask for explanations (like it's a bad habit to call "association[something]). Then I got it :D I'll edit to raise the ambiguity ;) Commented May 16, 2018 at 15:29
• Not sure wether that is better... =D Commented May 16, 2018 at 15:31
• It raises the ambiguity! Commented May 16, 2018 at 15:32

Given that you only want the second element, and there are no repeats, you could use FirstCase

secondElement[firstElement_] :=
FirstCase[{firstElement, other_} :> other]

secondElement[.1]@{{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}}
(* 5 *)

secondElement[0.4]@{{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}}
(* 6 *)

Pick [#[[All, 2]], #[[All, 1]], 0.4] &@lst


{6}

Pick [#[[All, 2]], #[[All, 1]], 0.1] &@lst


{5}

Pick [#[[All, 2]], #[[All, 1]], 0.1 | 0.4] &@lst


{5, 6}

list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};


Create an Association

look = <|Rule @@@ list|>


<|0.1 -> 5, 0.3 -> 18, 0.4 -> 6, 0.7 -> 9, 1.1 -> 5|>

Query one value

look[0.4]


6

Use Lookup to query more than one value

Lookup[look, {0.1, 0.4}]


{5, 6}

Handling of missing keys

Lookup[look, {0.1, 0.4, -1}]


{5, 6, Missing["KeyAbsent", -1]}

Lookup[look, {0.1, 0.4, -1}, Nothing]


{5, 6}

Use KeyTake to get keys and values (missing keys are ignored)

KeyTake[{0.1, 0.4, -1}] @ look


<|0.1 -> 5, 0.4 -> 6|>

list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};


Using AssociatePairs by Taliesin Beynon

Associate = ResourceFunction["AssociatePairs"];

asc = Associate[list]


<|0.1 -> 5, 0.3 -> 18, 0.4 -> 6, 0.7 -> 9, 1.1 -> 5|>

asc[0.4]


6

Lookup[asc, {0.4, 0.1}]


{6, 5}

list = {{0.1, 5}, {0.3, 18}, {0.4, 6}, {0.7, 9}, {1.1, 5}};


Using ReplaceList:

f[list_List, vals_?AtomQ | vals : {__?NumericQ}] :=
If[Length@# == 1, First@#, #] &@
ReplaceList[list, {___, s : {Alternatives @@ vals, b_}, ___} :> b]

f[list,0.5]


6

f[list, {0.4, 0.1}]


{6, 5}`