Cut out list values greater than a number

Any way to make a function that takes a list and a value and returns the list, but without entries larger than the value?

x = [5,15,25,35]

n = 21

f(x,n) = [5, 15]

Lists can be filtered by conditions. In this case:

f(L,n) = L[L<n]

You don’t need x as a parameter if you don’t intend to use multiple lists in the function:

f(n) = x[x<n]

Or if you don’t intend to have different values for n either:

x_{filtered} = x[x<n]

The problem with this is that it leaves behind undefined values. However, I discovered a solution that worked for my purposes.

f(L,n) = -(sort(-(L[L<n])))[1]

While you cannot use min() or max() on lists with undefined values without it always returning undefined, you can use sort(). The goal of this was to get the max for me, so I was trying to get rid of the undefined values all together. However, this works as well.

Would you mind sharing a graph keeping the undefined values? I don’t see that behavior, but maybe it’s just how I made my undefined values.

Huh, that’s really odd. Either I made a mistake, or the behavior has totally changed since last time I tried it. I can’t reproduce the issue I was having.

Maybe if you were using greater than? Infinity shows as undefined, so if you include <infinity in your condition (or >-infinity), it filters those undefined elements out:

Found the issue. I was trying to take the maximum of an empty list and mistaking it returning undefined as there being undefined values in the list.