Remove Falsy Values in Python

Beginner

This tutorial is from open-source community. Access the source code

Introduction

In this challenge, you need to write a Python function that removes falsy values from a list.

Compact 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 ]

Summary

In this challenge, you have learned how to remove falsy values from a list using the filter() function in Python.