From 5c7b7751d5c8665fb190d2f55c6af14d46de1ea6 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 20 Aug 2019 15:54:50 +0300 Subject: [PATCH] Add none snippet --- snippets/none.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/none.md diff --git a/snippets/none.md b/snippets/none.md new file mode 100644 index 000000000..b9644e197 --- /dev/null +++ b/snippets/none.md @@ -0,0 +1,22 @@ +--- +title: none +tags: list,function,intermediate +--- + +Returns `False` if the provided function returns `True` for at least one element in the list, `True` otherwise. + +Iterate over the elements of the list to test if every element in the list returns `False` based on `fn`. +Omit the seconds argument, `fn`, to check if all elements are `False`. + +```py +def none(lst, fn=lambda x: not not x): + for el in lst: + if fn(el): + return False + return True +``` + +```py +none([0, 1, 2, 0], lambda x: x >= 2 ) # False +none([0, 0, 0]) # True +```