6
$\begingroup$

I have a list containing some other lists having different lengths. I want to remove some of the lists with a certain length. For example, I have

a = {{0, 5, 2, 15, 6, 9, 3}, {1}, {5, 5, 5}, {4, 4, 5, 6}, {5}};

I want to remove some sub-lists of a having length 1. I mean, I want the result to be as

{{0, 5, 2, 15, 6, 9, 3}, {5, 5, 5}, {4, 4, 5, 6}}

How can I do that using Mathematica?

$\endgroup$
7
$\begingroup$
Select[a, Length@# != 1 &]

{{0, 5, 2, 15, 6, 9, 3}, {5, 5, 5}, {4, 4, 5, 6}}

Also

GeneralUtilities`Discard[a, Length[#] == 1 &]

{{0, 5, 2, 15, 6, 9, 3}, {5, 5, 5}, {4, 4, 5, 6}}

and

DeleteCases[a, _?(Length@# == 1 &)]

{{0, 5, 2, 15, 6, 9, 3}, {5, 5, 5}, {4, 4, 5, 6}}

$\endgroup$
6
$\begingroup$

This might be faster than klgr's solutions on large lists:

Pick[a, Unitize[Length /@ a - 1], 1]

Select and DeleteCases are known to be kind of slow.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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