Back to Each

Iterators · Each · Practice Question

Filter Sublists by Length

Sign in to track your progress

Question

Given the following list of lists:

l:(1 2 3 4;7 2 4 1;1 3 5 1 8;0 8;8 2 4)

Return only the sublists that have 3 or more elements.

Expected result:

1 2 3 4
7 2 4 1
1 3 5 1 8
8 2 4

Solution

l where 3<=count each l

Explanation

The each iterator:

'

applies a function independently to every item of a list — one item at a time, with no relationship required between items. Here it applies count to each sublist of l:

q)count each l
4 4 5 2 3

The comparison:

q)3<=count each l
11101b

produces a boolean mask which, used with where, keeps only the sublists with 3 or more elements — dropping 0 8, which has only 2.

each is the right tool whenever a function needs to run independently against every item of a list.