# How to select the first "lowest" (first valley) number in a list?

I would to select the first valley of the list of coordinates below. It should be {3, 1}.

list = {{1, 7}, {2, 4}, {3, 1}, {4, 4}, {5, 9}, {6, 8}, {7, 3}, {8, 1}, {9, 4}, {10, 9}};


I thought it could be done like this

Select[list, #1[[2]] > #2[[2]] &, 1]


but that does not work. How can it be done then?

• No, its the second coordinate that has to be lower than the previous 2.-coordinate... Apr 20 '14 at 12:02

list = {{1, 7}, {2, 4}, {3, 1}, {4, 4}, {5, 9}, {6, 8}, {7, 3}, {8,  1},{9, 4}, {10, 9}};
Last@First@Split[list, Last[#1] > Last[#2] &]
(* {3,1} *)


or

Catch[Fold[If[#2[[2]] > #1[[2]], Throw[#1], #2] &, First@list, Rest@list]]
(* {3,1} *)


or

First@NestWhile[Rest, list, #2[[1, 2]] < #1[[1, 2]] &, 2, Infinity, -1]
(* {3,1} *)

• This seems to work. Thank you! Apr 20 '14 at 12:03
• But I do not understand why the option of comparison between the elements of the list is not possible in Select[] and Position[], but only in Sort[] and Split[] as you propose... Apr 20 '14 at 12:09
• @SimonLausen, my pleasure.
– kglr
Apr 20 '14 at 12:09
• For Select the selection test function takes a single argument; it is applied to each element sepearately to decide whether the element qualifies for selection. For Split, Sort, Union ... the test function takes two arguments.
– kglr
Apr 20 '14 at 12:16

FYI: Select examines only one element at a time. To use Select in the way you want, Partition the list into adjacent pairs:

list = {{1, 7}, {2, 4}, {3, 1}, {4, 4}, {5, 9}, {6, 8}, {7, 3}, {8, 1}, {9, 4}, {10, 9}};

Select[Partition[list, 2, 1], #[[2, 2]] > #[[1, 2]] &, 1][[1, 1]]
(* {3, 1} *)