2
$\begingroup$

I have a list with several sublists containing either strings or integers. I'd like to delete all lists with strings.

The following list:

list = {{1, 2, 3, 4}, {2, 4, 5, 6, 6, 7}, {1, 23}, {"SomeString"}}

should become:

{{1, 2, 3, 4}, {2, 4, 5, 6, 6, 7}, {1, 23}}
$\endgroup$

5 Answers 5

4
$\begingroup$

This is an application for DeleteCases:

list = {{1, 2, 3, 4}, {2, 4, 5, 6, 6, 7}, {1, 23}, {"SomeString"}}

DeleteCases[list, {_String}]
$\endgroup$
2
  • $\begingroup$ Here you were faster... I would do the same, but with __String instead $\endgroup$
    – Murta
    Feb 8, 2015 at 22:16
  • $\begingroup$ :-). The field is now up to you. In Germany time to go to bed now, so keep on ... $\endgroup$
    – mgamer
    Feb 8, 2015 at 22:18
3
$\begingroup$

This removes all lists that contain at least one string, not just lists consisting of a single string:

Cases[list, _?(FreeQ[_String])]

(*  {{1, 2, 3, 4}, {2, 4, 5, 6, 6, 7}, {1, 23}}  *)
$\endgroup$
2
$\begingroup$
Select[list, FreeQ[_String]]

{{1, 2, 3, 4}, {2, 4, 5, 6, 6, 7}, {1, 23}}

FreeQ is going to filter lists with any strings. In this particular case, it works well, but this may or may not be useful in all situations.

$\endgroup$
2
$\begingroup$

Using GroupBy and Lookup:

Lookup[GroupBy[list3, AllTrue@StringQ], False]

(*{{1, 2, 3, 4}, {2, 4, 5, 6, 6, 7}, {1, 23}}*)
$\endgroup$
2
$\begingroup$
list = {{1, 2, 3, 4}, {2, 4, 5, 6, 6, 7}, {1, 23}, {"SomeString"}};

Using Cases

Cases[list, {__Integer}]

{{1, 2, 3, 4}, {2, 4, 5, 6, 6, 7}, {1, 23}}

$\endgroup$

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.