4
$\begingroup$

I have a the following list:

list = {
        {{0,1112},{1600,1110}}, {{0,693},{600,691}}, {{0,134},{1600,132}}, 
        {{1,188},{600,188}}, {{0,413},{1600,411}}, {{0,49},{600, 49}}
       }

I want to select all elements where list[[#, 2, 1]] == 600.

The resulting list should be:

{ {{0,693},{600,691}}, {{1,188},{600,188}}, {{0,49},{600,49}} }

I tried the following:

Select[list, list[[#, 2, 1]] == 600 &]

but I get errors:

Part::pkspec1: The expression {{0,1112},{1600,1110}} cannot be used as a part specification. ...

| improve this question | | | | |
$\endgroup$
  • 9
    $\begingroup$ Select[list, #[[2,1]]==600&] $\endgroup$ – user1066 Aug 9 '17 at 11:10
6
$\begingroup$
Cases[list, x : {_, {600, _}} :> x]

{{{0, 693}, {600, 691}}, {{1, 188}, {600, 188}}, {{0, 49}, {600, 49}}}

| improve this answer | | | | |
$\endgroup$
7
$\begingroup$

I'd probably go for @tomd's Select or @eldo's Cases, to be honest. But for completeness I thought I should add Pick:

Pick[#, #[[2, 1]] == 600 & /@ #] & @ list

(Edit: Or as @JM points out, a cleaner implementation is

Pick[#, #[[All, 2, 1]], 600] & @ list

)

Or you could do something like

If[#[[2, 1]] == 600, #, Nothing] & /@ list

(because who knows, someday you might need it). Both of which give

{{{0, 693}, {600, 691}}, {{1, 188}, {600, 188}}, {{0, 49}, {600, 49}}}

| improve this answer | | | | |
$\endgroup$

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