Sobes.tech
Junior — Senior

Filtering true list elements using list comprehension

livecode

Task condition

The task is to filter a given list to keep only the elements that are considered true (True) in Python. To solve this, you should use a short list comprehension that iterates over all elements of the original list and collects only those that satisfy the truthiness condition.

original_list = [0, 1, '', 'text', [], [1, 2], None, True, False]
filtered_list = [item for item in original_list if item]
print(filtered_list)  # outputs elements interpreted as True

As a result, filtered_list will contain only the elements that Python considers true, and all 'falsy' values (0, empty string, empty list, None, False, etc.) will be excluded.