3

I would like to know what would be the best way to check if a list is empty.

I know this question seems silly, but I realize that sometimes when you are working with others and others read your code, using certain functions may look better than others, and would be more understandable for people coming from different programming languages.

Say I have a list:

names = ["Bob", "Billy", "Samuel", "Adam", "Rob"]

This is one way I can check if the list is empty:

is_empty = bool(names)

This is another:

is_empty = any(names)

I do realize the any function checks if there is at least 1 true value inside a list. What would be the best and quickest way to check if the list is empty? Which would look the best and in which scenarios? Are there any faster ways that I do not know of?

Epic Gamer
  • 101
  • 1
  • 10
  • 3
    What's wrong with `is_empty = (names == [])`? – Scott Hunter Nov 18 '20 at 00:57
  • 3
    `len(names) == 0` is what seems most readable to me. – Telmo Trooper Nov 18 '20 at 00:59
  • 1
    `any(names)` doesn't work to check if it's empty. – Aplet123 Nov 18 '20 at 01:00
  • It returns a true or false value depending on if there are any elements in the list. – Epic Gamer Nov 18 '20 at 01:00
  • Try it with an empty list, and then a list with at least 1 element, and it will return a false value with the empty list, and a true value with the list with at least 1 element. – Epic Gamer Nov 18 '20 at 01:02
  • 2
    @EpicGamer It will also return False if the list is non-empty and contains only Falsey elements, so `any` won't work here. `any` doesn't check for "any elements in the list", it checks for "any *truthy* elements in the list". – Carcigenicate Nov 18 '20 at 01:05
  • The code isn't actually checking if the list is empty. That would be `if names:`. I assume there is a reason to record list state and use it elsewhere? In that case `is_empty = bool(names)` is the best way. It most closely follows what happens in an `if names:`. Most people would find it weird that the state is being bound to a variable in the first place IMHO. – tdelaney Nov 18 '20 at 01:08
  • Yes you are right. I did not realize that. So then would that mean that any() is good when you are checking whether or not a list consisting of non empty strings or non zero numbers is empty? – Epic Gamer Nov 18 '20 at 01:08
  • @EpicGamer - No, its role is to check if there are any True-like things in the list. Its possible that your list is constrained to true-like things and that `any` is the same as non-empty. But that is a puzzling way to do it when the list itself is falsy when empty. – tdelaney Nov 18 '20 at 01:12

1 Answers1

4

It's simplest to just use the implicit boolean method of lists:
if mylist: will branch True if mylist has contents, and False if mylist is empty. From the documentation, an empty list is considered to be False as in boolean evaluation the default __len__() method is called and it is equal to zero. This is the most pythonic and the fastest way to evaluate it, though mechanistically identical to if len(mylist) == 0:. The same applies to strings, tuples, lists, dictionaries, sets, and range().

Edit: With regards to one of the given examples, as noted by @Carcigenicate, any() will fail in select situations if the goal is purely to check if a list is empty. any() will return False "if the list is non-empty and contains only Falsey elements."
For example, any([False, 0]) will return False despite the list containing two objects.

Monomaniac
  • 145
  • 10
  • Additionally, if one isn't sure that `mylist` is a list, one may use `if mylist and type(mylist) is list: ...` or possibly, `if mylist and isintance(mylist, list): ...`. (The latter check is a bit more lenient, in that it'll accept subclasses of list.) – Sumukh Barve Nov 18 '20 at 11:59