Python USES any to determine whether an object is empty

  • 2020-04-02 14:21:20
  • OfStack

This example shows how python USES any to determine whether an object is empty. Share with you for your reference.

The specific implementation code is as follows:

>>> eth = {"eth0 " :"192.168.1.1 " }
>>> any(eth)
True
>>> eth = {}
>>> any(eth)
False

Determine if the list is empty

The traditional way:

if len(mylist):
    # Do something with my list
else:
    # The list is empty

Since an empty list itself is equal to False, we can directly:
if mylist:
    # Do something with my list
else:
    # The list is empty

I hope this article has helped you with your Python programming.


Related articles: