3
$\begingroup$

I have a list:

lis = {{"a","b"},{"c","d","prq"},{"e","f","wxy *1"},{"g","h","wxy *2"}}

I would like to delete all elements that have an "*", to make:

res = {{"a","b"},{"c","d","prq"},{"e","f"},{"g","h"}}

This would seem to be a job for StringContainsQ and DeleteCases, but am not sure how to do this. Thanks for suggestions.

$\endgroup$

1 Answer 1

4
$\begingroup$
DeleteCases[lis, _String?(StringContainsQ["*"]), All]
{{"a", "b"}, {"c", "d", "prq"}, {"e", "f"}, {"g", "h"}}
lis /. _String?(StringContainsQ@"*") :> Nothing
{{"a", "b"}, {"c", "d", "prq"}, {"e", "f"}, {"g", "h"}}
Select[StringFreeQ["*"]] /@ lis
{{"a", "b"}, {"c", "d", "prq"}, {"e", "f"}, {"g", "h"}}
Pick[lis, StringFreeQ["*"] /@ lis]
{{"a", "b"}, {"c", "d", "prq"}, {"e", "f"}, {"g", "h"}}
$\endgroup$
1
  • $\begingroup$ Thanks @kglr for the different options, will do some timing tests on larger data sets. $\endgroup$
    – Suite401
    Commented Sep 11, 2021 at 3:49

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.