# Translate selection from one list to another

I wanted to know the most efficient and less resource consuming way to do the following: first, use the Select function to filter some values out of some list, e.g.

a={1,2,3,4,5,6};
a2=Select[a,EvenQ];

Now, let's create another list of the same length, b={a,b,c,d,e,f}. I want to pick exactly the same elements that I picked in a, i.e. b2={b,d,f}. Is there a built-in function that allows me to do that? If not, what would be the quickest way to do that (in terms of computing time)?

Edit: after some useful responses, I want to clarify something: I still want to do the selection in list a, i.e. I want to know if there is a way to do the selection in both lists a,b in one single step. What I've thought of is combining both lists with c=Transpose[{a,b}] and then applying Select with the condition on c[[i,1]], where i represents the index of either aor b. However, if there is any other solution, I'd like to hear it.

An update

Using the third argument of GroupBy, PositionIndex and Extract:

Extract[GroupBy[PositionIndex[blist], EvenQ, Keys], Key[{True}]]

(*{b, d, f}*)

Or using AssociationThread, GroupBy and Lookup:

(*{b, d, f}*)

You could use Pick[]:

Pick[{a, b, c, d, e, f}, EvenQ[{1, 2, 3, 4, 5, 6}]]
(*  {b, d, f}  *)

Timing examples (numerical methods on packed arrays are naturally fastest, but data is not always numerical and packed):

biglist = Range[10^7];
Pick[biglist, EvenQ[biglist]]; // RepeatedTiming
(*  {0.454526, Null}  *)

biglist = DeveloperFromPackedArray@Range[10^7];
Pick[biglist, EvenQ[biglist]]; // RepeatedTiming
(*  {0.28087, Null}  *)

biglist = Range[10^7];
Pick[biglist, Mod[biglist, 2], 0]; // RepeatedTiming
(*  {0.076138, Null}  *)

biglist = DeveloperFromPackedArray@Range[10^7];
Pick[biglist, Mod[biglist, 2], 0]; // RepeatedTiming
(*  {1.35772, Null}  *)

Or use Association.

Clear[list1, list2, asso, list];
list1 = {1, 2, 3, 4, 5, 6};
list2 = {a, b, c, d, e, f};
list = Select[asso, EvenQ]
Keys@list

<|b -> 2, d -> 4, f -> 6|>

{b, d, f}

There can be many solutions, and here is one of them.

Clear[alist, blist]
alist = {1, 2, 3, 4, 5, 6};
blist = {a, b, c, d, e, f};
Extract[blist, Position[alist, x_ /; EvenQ[x]]]

Another one could be:

Pick[blist, EvenQ[alist]]

Using Transpose:

Select[Transpose[{alist, blist}], EvenQ@First@# &][[All, -1]]

Result

{b, d, f}

The most minimal way -I think- to pick the even elements is to use the step specification in the Part like so:

list = {a, b, c, d, e, f};

list[[2 ;; -1 ;; 2]]

{b, d, f}

• Sorry, but that's not what I asked, it was just an example. My list has various nesting levels and the conditions I want to impose are fairly complicated, but the basics are the same. Sep 11, 2022 at 12:38
• @Marcosko "My list has various nesting levels" -- If you mean you wish to pick out elements at various levels, this will make some solutions, such as your own Select[], invalid on the actual use case. -- The limitation in this answer seems to be that the solution does not depend on the a list or the test function and assumes they are always consecutive integers and EvenQ. Sep 11, 2022 at 13:00
• @Marcosko please see the comment by Michael E2 as well
– bmf
Sep 11, 2022 at 13:09
• @MichaelE2 maybe I read the description of the problem too quickly, but I thought that the objective was to single out the letters that correspond to the even numbers. After your comment and reading again, indeed the suggested solution here does not generalize nicely for generic associations
– bmf
Sep 11, 2022 at 13:10
• @MichaelE2, that's correct, for now I just want to select at the first level. My point was that the solution proposed in this post wasn't general enough to be useful for me. Sep 11, 2022 at 13:11

KeySelect is another possibility

al = {1, 2, 3, 4, 5, 6};
bl = {a, b, c, d, e, f};