any() Method in Python Programming

Any() Method In Python

The any() method in Python is another built-in method. We saw earlier the working and the usage of the all() method in Python. The any() method is very similar to that method. It tells us about the nature of any iterable object. Let us understand what the method actually is and how it works.

How any() Method in Python works

The any() method in Python checks whether any of the elements of an iterable object like an array, list, tuple, etc. are true or not.

If any of them is true, the method directly returns ‘True‘ and ‘False‘ if none of them is true. The any() method also returns a ‘False‘ is the provided iterable object is empty. Take a closer look at the syntax below,

Syntax,

any( iterable_object_name )

Here,

  • iterable_object_name, as the name suggests is the name of the object for which the checking is to be done
  • The method will return ‘True‘ if and only if any of the iterable object elements is true
  • On the other hand, the method will return ‘False‘ if all the elements are false or the iterable is an empty one

Examples of any() method in Python

The below-given code tries to illustrate the use and working of the built-in Python method any() in the best way possible,

# python any() example
print("any() in Python:")

# Defining different type of variables

list1 = ['John','Arya','Daniel','Sophie']
list2 = [0, 1, 1, 1, 0]
dictionary1 = {1: "True", 2: "False"}
dictionary2 = {0: "False", 1: "True"}
tpl1 = (0, 0, 0, 0)

# Evaluating the variables with the any() method.

print("any() results for various variables and objects:")
print("list1=['John','Arya','Daniel','Sophie']:", any(list1))
print("list2=[0,1,1,1,0]:", any(list2))
print("tpl1=(0,0,0,0):", any(tpl1))
print("dictionary1:", any(dictionary1))
print("dictionary2:", any(dictionary2))

# Testing any() method's evaluation for empty objects

dict_empt = {}
list_empt = []
print("any() results for empty objects:")
print("dict_empt:", any(list_empt))
print("list_empt:", any(dict_empt))

Output:

Any Method Testing Example
any() Method Testing Example

Starting for the different iterable objects we have considered for this example, we have used lists, tuple, as well as dictionaries. But the any() method is applicable for any iterable object and not specifically what we have used.

Understand the code,

  • Since list1 has all truthy values, the any() method for list1 returns True,
  • Similarly, list2 contains three(3) non-zero, non-falsy values hence in this case too any() returns True
  • For tuple tpl1, the method returns False since all the elements are 0 and evaluate to false
  • Both the dictionaries, dictionary1 and dictionary2 have at least one Truthy value. So we get our desired output, that is True
  • For empty iterable objects, as mentioned earlier the any() method returns False for both empty list and dictionary

Difference between the any() and all() method in Python

A quick, summarized difference between all() and any() method in Python:

  • any() checks whether at least one of the elements of an iterable is Truthy
  • all() checks whether all of the elements of an iterable are Truthy
  • For empty objects any() returns False whereas, all() returns true

Conclusion

In this tutorial, we learned about the built-in any() method in Python, how it works and how it interacts with various iterable objects.

You can also have a look at some of our previous write-ups on built-in methods in Python which will help you understand the different methods that are always available for use in Python without the need to import anything.

References