# How to select elements from a list of pairs based on 2nd element of the pair [closed]

I have a large list of arrays of the following form

{{{0, 0, 0}, 1}, {{2, 2, 2}, 0}, {{2, 2, 0}, 0}, {{2, 2, -2},
0}, {{2, 0, 2}, 0}, {{2, 0, 0}, 1}, {{2, 0, -2}, 0}, {{2, -2, 2},
0}, {{2, -2, 0}, 0}, {{2, -2, -2}, 0}}


with the second entity in each element is either 0 or 1. I need to create a do loop that search within each array for those elements with second entity equals 1 and return the result as a new array with all other elements eliminated. If none of the array element satisfy this requirement I need the result to be returned as 0. For example for the above array I want to get the result as

{{0, 0, 0}, {2, 0, 0}}


because these two elements have second entity equals 1.. How can I do that?

• No need for loops, use either Cases[] or Select[]. Alternatively, use GatherBy[] or GroupBy[] and pick out what you need. – J. M.'s ennui Jun 22 '16 at 16:21
• Cases[lst, {a_List, 1} :> a] – march Jun 22 '16 at 16:22
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Jun 22 '16 at 17:49

Never forget Pick for problems involving picking elements from a list.

data =
{{{0, 0, 0}, 1}, {{2, 2, 2}, 0}, {{2, 2, 0}, 0}, {{2, 2, -2}, 0},
{{2, 0, 2}, 0}, {{2, 0, 0}, 1}, {{2, 0, -2}, 0}, {{2, -2, 2}, 0},
{{2, -2, 0}, 0}, {{2, -2, -2}, 0}};
Pick[data, Last /@ data, 1]


{{{0, 0, 0}, 1}, {{2, 0, 0}, 1}}

• Or, without Map: Pick[#, #[[All, 2]], 1] &@data – user1066 Jun 22 '16 at 21:08
Cases[{{{0, 0, 0}, 1}, {{2, 2, 2}, 0}, {{2, 2, 0}, 0}, {{2, 2, -2},
0}, {{2, 0, 2}, 0}, {{2, 0, 0}, 1}, {{2, 0, -2}, 0}, {{2, -2, 2},
0}, {{2, -2, 0}, 0}, {{2, -2, -2}, 0}}, {list_, 1} :> list]

(*{{0, 0, 0}, {2, 0, 0}}*)

• Thanks for the quick reply.. What if I want to get 0 if none of the elements satisfy the condition? – eftrsd Jun 22 '16 at 16:26
• Just add /. {} -> 0 at the end. – march Jun 22 '16 at 16:33
• Brilliant! Thanks – eftrsd Jun 22 '16 at 16:58

## Example

Data

list = {{{0, 0, 0}, 1}, {{2, 2, 2}, 0}, {{2, 2, 0}, 0}, {{2, 2, -2}, 0}, {{2, 0, 2}, 0}, {{2, 0, 0}, 1}, {{2, 0, -2}, 0}, {{2, -2, 2}, 0}, {{2, -2, 0}, 0}, {{2, -2, -2}, 0}}


Code

Select[list, #[[2]] == 1 &][[All, 1]] (*For cases 1*)
Select[list, #[[2]] == 2 &][[All, 1]] (*For cases 2*)


Output

{{0, 0, 0}, {2, 0, 0}}

Reference

You can also use replacement rules. FYI, this method is usually slow on very large lists.

list/. {
{{_, _, _}, 0} -> Sequence[],
{x : {_, _, _}, 1} :> x
}


Which gives:

{{0, 0, 0}, {2, 0, 0}}


Just some variants.

If only want first element:

Reap[Sow @@@ data, 1, #2 &][[2, 1]]
1 /. GroupBy[data, Last -> First]


both yield:

{{0, 0, 0}, {2, 0, 0}}


If whole element:

1 /. GroupBy[data, Last]


yields:

{{{0, 0, 0}, 1}, {{2, 0, 0}, 1}}