2
$\begingroup$

I have a list such as {{abc,1},{def,2},{ghi,1},{jkl,3}}.

I would like to extract elements that have 1 as the second element (ignoring those that have 2 or 3 as their second element).

In the case above, I want a command to extract the elements: "abc" and "ghi", based on the criteria that their second elements are equalled to 1.

How should I go about doing so?

$\endgroup$
  • $\begingroup$ Have a look at Cases. $\endgroup$ – b.gates.you.know.what Sep 10 at 15:52
  • $\begingroup$ This works! Thanks! $\endgroup$ – reemodels Sep 10 at 15:56
3
$\begingroup$
Cases[{{abc, 1}, {def, 2}, {ghi, 1}, {jkl, 3}}, {_, 1}]

The command above solved it.

$\endgroup$
  • $\begingroup$ Select[{{abc, 1}, {def, 2}, {ghi, 1}, {jkl, 3}}, Last@# == 1 &] $\endgroup$ – Rohit Namjoshi Sep 10 at 16:05

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