From 783c15b8ad059dcc84938321fea7238be41cc811 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 10 Dec 2022 17:02:33 +0200 Subject: [PATCH] Add a new python question --- blog_posts/python-empty-list.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 blog_posts/python-empty-list.md diff --git a/blog_posts/python-empty-list.md b/blog_posts/python-empty-list.md new file mode 100644 index 000000000..51561c4e5 --- /dev/null +++ b/blog_posts/python-empty-list.md @@ -0,0 +1,24 @@ +--- +title: How can I check if a Python list is empty? +shortTitle: Empty list +type: question +tags: python,list +author: chalarangelo +cover: blog_images/salad-2.jpg +excerpt: There's a good way to test the emptiness of a Python list and a better one. Which one are you using? +firstSeen: 2023-01-15T05:00:00-04:00 +--- + +Checking the emptiness of a Python list is rather easy using the `len()` function. Yet, there's another technique that works on all types of sequences and collections. This is based on the truth value testing of the sequence or collection itself. + +By default, a Python object is considered truthy unless its class defines either a `__bool__()` or a `__len__()` method that returns `False` or `0` respectively. Python's built-in objects, such as tuples, lists, strings, dictioners, sets and ranges all implement a `__len__()` method. This menas that truth value testing of these objects will return `False` if they are empty, and `True` otherwise. + +Based on these observations, all you need to check if a Python list is empty is to test its truth value, using the `not` operator. + +```py +x = [] +not x # True + +y = [1, 2] +not y # False +```