Say I have a list as below:

list = {{"A_OB021_Mp", "hsa-miR-30b_000602", 23.724894`},{"A_OB021_Mp", "hsa-miR-29a_002112", 30.242905`},{"A_OC021_Mp", "hsa-miR-485-3p_001277", "Undetermined"},{"A_OC021_Mp", "mma-miR-484_001821", 26.052717`},{"A_OB021_Mp", "hsa-miR-380-3p_000569", "Undetermined"},{"A_OE021_Mp", "hsa-miR-453_002318", "Undetermined"},{"A_OE021_Mp", "hsa-miR-485-5p_001036", "Undetermined"},{"A_OB021_Mp", "U6 rRNA_001973", 17.669361`},

{"A_OH021_Mp", "hsa-miR-449_001030", "Undetermined"},{"A_OH021_Mp", "hsa-miR-302b_000531", "Undetermined"},{"A_U6021_Mp", "hsa-miR-411_001610", "Undetermined"},{"A_U6019_Mp", "hsa-miR-324-5p_000539", "Undetermined"},{"A_OB021_Mp", "hsa-miR-30c_000419", 25.73165`}}

If I want to filter the list to find strings with "U6" I use the following code:

Position[list,s_String /;StringMatchQ[s,"*U6*"]] // (int = #) &;Table[Extract[list, int[[i]][[1]]], {i,1,int // Length}] // (data = #) &

which works perfectly fine. How do I filter via two strings? I wanted to do "OB"&&"U6" but it does not work. Or alternatively, how do I exclude a certain string? e.g. extract row with a string having "U6", except when string "hsa" is present in the same raw.

For now I just iterate the above code using the outcome of the first iteration as the input for the second iteration, but it is not efficient for large datasets.

  • $\begingroup$ No Mathematica on this machine to test, what happens if you try /;And[StringMatchQ[s,"*U6*"],StringMatchQ[s,"*OB*"]]]? And have you validated your assertion it is not efficient for large datasets? $\endgroup$ Commented Dec 2, 2021 at 15:10
  • $\begingroup$ Try this: MapAt[StringContainsQ[#, "U6" | "OB"] &, alist, {All, 1 ;; 2}] Also your list needs a brace at the end instead of a comma. $\endgroup$
    – Syed
    Commented Dec 2, 2021 at 15:18

3 Answers 3


A slightly different approach:

list = {
   {"A_OB021_Mp", "hsa-miR-30b_000602", 23.724894`},
   {"A_OB021_Mp", "hsa-miR-29a_002112", 30.242905`},
   {"A_OC021_Mp", "hsa-miR-485-3p_001277", "Undetermined"},
   {"A_OC021_Mp", "mma-miR-484_001821", 26.052717`},
   {"A_OB021_Mp", "hsa-miR-380-3p_000569", "Undetermined"},
   {"A_OE021_Mp", "hsa-miR-453_002318", "Undetermined"},
   {"A_OE021_Mp", "hsa-miR-485-5p_001036", "Undetermined"},
   {"A_OB021_Mp", "U6 rRNA_001973", 17.669361`}};

 StringMatchQ[#[[1]], "*OB*"] && StringMatchQ[#[[2]], "*U6*"] &]

(* {{"A_OB021_Mp", "U6 rRNA_001973", 17.6694}} *)

 StringMatchQ[#[[2]], "*miR*"] && ! StringMatchQ[#[[2]], "*hsa*"] &]

(* {{"A_OC021_Mp", "mma-miR-484_001821", 26.0527}} *)
Select[Not @* FreeQ[s_String?(StringContainsQ["U6"])]] @ list

enter image description here

Select[Not @* FreeQ[s_String?(StringContainsQ["U6" | "OB"])]] @ list

enter image description here


Define a utility function g1. It detects if "U6" or "OB" appear in the first two sub-elements of a sublist.

g1[k_List] := Module[{c1},
  c1 = StringContainsQ[#, "U6" | "OB"] & /@ k[[1 ;; 2]];
  MemberQ[c1, True]


pos = g1 /@ alist

{True, True, False, False, True, False, False, True}

Pick[alist, g1 /@ alist]

$$\left( \begin{array}{ccc} \text{+A$\_$OB021$\_$Mp} & \text{hsa-miR-30b$\_$000602} & 23.7249 \\ \text{A$\_$OB021$\_$Mp} & \text{hsa-miR-29a$\_$002112} & 30.2429 \\ \text{A$\_$OB021$\_$Mp} & \text{hsa-miR-380-3p$\_$000569} & \text{Undetermined} \\ \text{A$\_$OB021$\_$Mp} & \text{U6 rRNA$\_$001973} & 17.6694 \\ \end{array} \right)$$

Define g2 for the other task.

g2[k_List] := Module[{c1},
  c1 = (StringContainsQ[#, "U6"] &&
       StringFreeQ[#, "hsa"]) &
    /@ k[[1 ;; 2]];
  MemberQ[c1, True]

Usage is the same:

pos = g2 /@ alist
Pick[alist, g2 /@ alist]

$$\left( \begin{array}{ccc} \text{A$\_$OB021$\_$Mp} & \text{U6 rRNA$\_$001973} & 17.6694 \\ \end{array} \right)$$

For both cases, pos is being calculated as a visual/debugging cue only.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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