Introduction
In this challenge, you need to write a Python function that removes falsy values from a list.
This tutorial is from open-source community. Access the source code
In this challenge, you need to write a Python function that removes falsy values from a list.
Write a function compact(lst) that takes a list as an argument and returns a new list with all falsy values removed. Falsy values include False, None, 0, and "".
To solve this problem, you can use the filter() function to filter out falsy values from the list.
def compact(lst):
return list(filter(None, lst))
compact([0, 1, False, 2, '', 3, 'a', 's', 34]) ## [ 1, 2, 3, 'a', 's', 34 ]
In this challenge, you have learned how to remove falsy values from a list using the filter() function in Python.