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

5
$\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$ Commented 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$ Commented Feb 8, 2015 at 22:18
4
$\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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.