Files
30-seconds-of-code/snippets/python/s/compact.md
2023-05-07 16:07:29 +03:00

391 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Compact list snippet python
list
new-plant 2020-11-02T19:27:07+02:00

Removes falsy values from a list.

  • Use filter() to filter out falsy values (False, None, 0, and "").
def compact(lst):
  return list(filter(None, lst))
compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ]