Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Square a number using lambdaE02. Convert a list of strings to integers with mapE03. Filter out falsy values from a listE

Problem No 3

Filter out falsy values from a list

Easy

10 minute session

Summary

Remove all falsy values from a list while preserving order.

Problem statement

Write a function filter_falsy(lst) that accepts a list of arbitrary values and returns a new list containing only the truthy values (i.e., values where bool(value) is True). Preserve the original order of truthy items. Implement the filtering using filter or a similar functional approach.

Task

Use filter (or an equivalent functional approach) to remove all values that evaluate to False in a boolean context from a list.

Examples

Filter common falsy values

Input

filter_falsy([0, 1, '', 'a', [], [1], None, False])

Output

[1, 'a', [1]]

Explanation

0, empty string, empty list, None, and False are falsy and removed.

Input format

A list of values of any types (numbers, strings, lists, None, booleans, etc.).

Output format

A list containing only truthy values from the input, in the same order.

Constraints

The input list length will be between 0 and 1000. Do not modify the input list in-place; return a new list.

Samples

Sample input 0

filter_falsy([False, True, 0.0, 0.1])

Sample output 0

[True, 0.1]

Explanation 0

False and 0.0 are removed; True and 0.1 remain.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:49 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.