Cannot use Part in Pure Function [duplicate]

I need to select some lists in the nested list of form of {{1,2,3},{2,3,4}}. I have used Select to complete my work. But when I tried Position, it doesn't work. For example,

 Flatten@@Position[{{1, 2, 3}, {2, 3, 4}}, _?(# == {1, 2, 3} &)]


gives me the correct answer of $\{1\}$,but when I use

Flatten@@Position[{{1, 2, 3}, {2, 3, 4}}, _?(#[[1;;2]] == {1, 2} &)]


returns the error

Part::take: Cannot take positions 1 through 2 in List. >>
Part::take: Cannot take positions 1 through 2 in List. >>
Part::take: Cannot take positions 1 through 3 in 1 >>

It seems that it is not working with lists, and it is also trying to apply to higher level. Why is it and how to fix that?

marked as duplicate by Mr.Wizard♦Feb 5 '15 at 10:53

• I did not realize this was a duplicate until after I answered. I am converting my answer to Community Wiki so that I do not gain additional "reputation" from it, and so that others can add anything that would be helpful. – Mr.Wizard Feb 5 '15 at 10:54

Position by default looks at all parts of the expression, including those that do not have sub-parts to extract. You can use levelspec and the Option Heads to restrict what it tests:

Position[{{1, 2, 3}, {2, 3, 4}}, _?(#[[1 ;; 2]] == {1, 2} &), {1}, Heads -> False]

{{1}}


More generally you can simply use a pattern like this:

Position[{{1, 2, 3}, {2, 3, 4}}, _[1, 2, ___]]

{{1}}


Position[{foo[1, 2, 3], bar[2, 3, 4]}, _[1, 2, ___]]

{{1}}