1
$\begingroup$

I have several lists of tuples in the following shape:

l1 = {{1, 134}, {2, 314}, {3, 164}}

I do want to select, from each list, list elements fulfilling a certain criteria. I am using the following pure function for this (applied here to my first list):

In[508]:= Select[l1, #[[1]] > 2 &]

Out[508]= {{3, 164}}

How can I apply this function to several lists at the same time?

Silly question first: how would you best describe this task? That might help me searching for it in the forums. So far I found no good way of doing it (or possibly have simply not understood it if I came across it).

I would be grateful if anyone could point me the right way. I find the pure function annotation a bit confusing still, so any help is greatly appreciated.

thanks!

mondo

Edit addressing Szabolcs' comment: I do have a solution to filter a single list. I want to filter through all my lists though, without having to apply a for loop (if possible).

$\endgroup$
  • 1
    $\begingroup$ Can you clarify the question a bit? It seems you already have a solution. Are you just asking what this operation is called? $\endgroup$ – Szabolcs Jul 27 '15 at 9:43
  • $\begingroup$ this search will find similar questions on this site. $\endgroup$ – Sjoerd C. de Vries Jul 27 '15 at 9:45
  • $\begingroup$ @Szabolcs, added edit above. $\endgroup$ – mondo Jul 27 '15 at 10:02
  • $\begingroup$ Map, perhaps? (Use Map to apply Select to each list in your list of lists. Select does not have a level specification, so you can't do it without some sort of loop. Maybe some don't think of Map as a loop.) $\endgroup$ – Michael E2 Jul 27 '15 at 10:06
  • $\begingroup$ Thanks Michael - Mapping was what I had in mind, but I couldn't work out the syntax. MikeLimaOscar's answer below is exactly what I had in mind. Thank you all, this has been recurring in my code and given me a headache for a long time... $\endgroup$ – mondo Jul 27 '15 at 10:26
1
$\begingroup$

You say that you have several lists of tuples so I assume you wish to map your Select to them all like:

Select[#, #[[1]] > 2 &] & /@ {l1, l2, l3}

If you find the nesting of the slots, #, confusing then you could use the explicit form of Function, e.g.

Function[{lst}, Select[lst, Function[{elem}, elem[[1]] > 2]]] /@ {l1, l2, l3}
$\endgroup$
  • $\begingroup$ Mike, thanks a lot! this is exactly what I had in mind. Can you quickly elaborate why I do not need to add indices to the Slots? I.e., why it is not something along the lines of Select[#1, #2[[1]] > 2 &] & /@ {l1, l2, l3} ? I always found it a bit hard to understand why my original Select function was understood without adding what I wanted # to resemble... $\endgroup$ – mondo Jul 27 '15 at 10:21
  • $\begingroup$ @mondo Slot indices access different arguments to a particular anonymous function not different levels in nested functions. This sort of thing is clearer when you use the Function form. $\endgroup$ – MikeLimaOscar Jul 27 '15 at 10:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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