Python any() function Examples [Beginners]

 

Introduction to Python any() function

Python any() function is a built-in function that is used to check the items of multiple data type objects like lists, tuples, dictionaries, etc that contains true, then the python any() function will return true as well. For example python, any() function will return true if any of the values in a given list is true. This function works like a logical OR condition that returns true if any one condition is true. In this tutorial, we will learn about python any() function by taking different examples.  We will also cover how we can use python any() function to check if a string has letters or numeric values and will see how we can use any function with multiple conditions. All in all, this tutorial provides a full understanding of python any() function.

 

Getting started with python any() function

As we already discussed, Python any() function is a built-in function that works very similar to logical OR operator. Here is a simple syntax of any function.

Advertisement
any(iterable_name)

the any() function takes and iterable as an argument and returns True if at least one of the items in the iterable is true. It returns True if bool(iterable_name) is True for any of the iterable. And return False if the bool(iterable_name) is False.

Now let us take some examples to see how actually python any() function works.

 

Examples of python any() function on strings

Any string value is considered as a True value for python any() function, except an empty string. Let us now take different cases. Let us say we have string1 which contains a word, string2 which contains numeric values as string type, string3 which contains symbols as strings. And let us apply python to any function and see what we get.

# string one
string1 = "Bashir"

# string two
string2 = "1234"

# string three
string3 = "@#$"

# apply python any() function
print(any(string1))
print(any(string2))
print(any(string3))

Output:

True
True
True

Notice that we get True for all three cases because in all three cases we had a valid string. Now let us take an empty string and a string containing only whitespace and see what we get if we apply python any() function on them.

# string one
string1 = ""

# string two

string2 = "  "

# apply python any() function
print(any(string1))
print(any(string2))

Output:

Advertisement
False
True

We get false in the first case because it was an empty string, while in the second case we get True because whitespaces in python are valid string types.
 

Examples of python any() function on lists

First, let us see how we can use python any() function on list variables. Here we will use three different kinds of lists. list1 is an empty list, list2 contains strings and list3 contains digits, and let us now apply any() function on all these lists and see what we get. See the example below:

# list one
list1 = []

# list two
list2 = ["b", "a", "s", "h"]

# list three
list3 = [1, 2, 3, 4]

# using any() function
print(any(list1))
print(any(list2))
print(any(list3))

Output:

False
True
True

Notice that except the first one which was an empty list, the rest all are True because they contain valid and True values. But a list containing all zeros values will also return False because in python zero is considered to be False. See the example below:

# list containing zeros
list1 = [0, 0, 0, 0, 0, 0, 0]

# python any() function
print(any(list1))

Output:

False

Now if a list containing zeros but not as numeric values but as strings, then it will return True because this time python will treat them as strings rather than a numeric value. See the example below:

# list containing zeros
list1 = ["0", "0", "0", "0", "0", "0", "0"]

# python any() function
print(any(list1))

Output:

True

 

Examples of python any() function on tuples

Python any() function works in a similar way on tuples as well. It will return True if any of the elements in the tuple is non-zero and if the tuple is not empty. let us confirm this assumption by taking three different kinds of tuples and apply the python any() function to them. Let us say we have tuple1 which is empty, tuple2 which contains strings, and tuple3 which contains digits, and see what we get if we apply any() function on these tuples. See the example below:

Advertisement
# tuple one
tuple1 = ()

# tuple two
tuple2 = ("B", "v", "r", "e")

# tuple three
tuple3 = (1, 2, 3,4, 5, 6)

# applying python any() function
print(any(tuple1))
print(any(tuple2))
print(any(tuple3))

Output:

False
True
True

Now let us apply the python any() function on a tuple containing zeros as elements. See the example below:

# tuple
tuple1 = (0, 0, 0, 0, 0,0, 0)

# python any() function
print(any(tuple1))

Output:

False

Now let us take an example of a tuple which contains different symbols and see what we get when we apply python any() function on it. See the example below:

# tuple
tuple1 = ("@", "%", "&")

# python any() function
print(any(tuple1))

Output:

True

We get true because they are considered to be valid elements and as we they are inside quotation marks, they are considered to be a string type. And even if we write zero inside strings and apply any function on it, we will get True. Even we will get True for an empty string having just a space inside quotation marks as well. See the example below:

# tuple
tuple1 = (" ")

# python any() function
print(any(tuple1))

Output:

True

 

Examples of python any() function on dictionaries

We already discussed different cases of lists and tuples, now let us apply python any() function on dictionaries by taking different scenarios. Let say we have dict1 which contains zero as key, dict2 has zero as value and dict3 contains strings and numbers as key-value pair. Now let us apply python any() function on these different dictionaries and see what we get. See the example below:

Advertisement
# dict one
dict1 = {0:"b"}

# dict two
dict2 = {"zero": 0}

# dict three
dict3 = {"key": 1}

# python any() function
print(any(dict1))
print(any(dict2))
print(any(dict3))

Output:

False
True
True

Notice that we only get False, where the key was zero, not the value. Now let us take the first case again, but this time let us put the zero inside quotation marks to make it a string. See the example below:

# dict one
dict1 = {"0":"b"}

# python any() function
print(any(dict1))

Output:

True

This time we get True because the python interpreter treats the zero in key as a string, rather than a numeric value.
 

Examples of checking digits in a string using python any() function

Now we already know the syntax of using python any() function and how we can apply it to different datatypes, let us take a practical example. We can use python any() function to check if there are any digits in a string or not. See the following example which returns true if string has digits and else returns false.

# strings
string1="bashir alam"
string2 = "bas124ir"

# checking for digits
checking1 = [char.isdigit() for char in string1]
checking2 = [char.isdigit() for char in string2]

# using any function
print(any(checking1))
print(any(checking2))

Output:

False
True

In the above example, the loop iterate through the string to access each character, and then isdigit() method checks if the string has any digits. the isdigit() return True if the character under test is a digit, else it returns False. checking1 and checking2 is actually a list of True and False, because for each character in the string, there is a corresponding truth value  if the character is a digit and False if the character is not a digit. See the following code where we print the list of True and False values as well.

# strings
string1="bashir alam"
string2 = "bas124ir"

# checking for digits
checking1 = [char.isdigit() for char in string1]
checking2 = [char.isdigit() for char in string2]
print(checking1)
print(checking2)

Output:

[False, False, False, False, False, False, False, False, False, False, False]
[False, False, False, True, True, True, False, False]

See that for the first one there is no any True value, that is why we got False in the previous example. And there are True values in the second one, and we got True in our previous example for this one.

 

Examples of checking letters in a string using python any() function

In a similar way, we can use python any() function to check if a letter is present in a string or not because we know that a string is anything written inside quotation marks( it can contains numeric values, letters, symbols, and many more). If the letter is present, it will return true else it will return False. See the example below which check the letters in a string.

# string one
string1 = "3444543"

# string two
string2 = "#$#%))"

# string three
string3 = "%#$sdfadfgfg"

# checking for letter
letter1 = [char.isalpha() for char in string1]
letter2 = [char.isalpha() for char in string2]
letter3 = [char.isalpha() for char in string3]

# printing
print(any(letter1))
print(any(letter2))
print(any(letter3))

Output:

False
False
True

Notice that we only get True for string3 where there were letters. Python any() function is very useful for validation purposes.

 

Examples of combining multiple conditions using python any() function

So far we have learned python any() function and had applied it to many different cases as well. In this section, we will learn how we can combine multiple conditions using python any function. Mainly we will use the if-else statement with AND and OR logic operations to combine different conditions. Let us say we have the following case, where we have three different strings containing different values. In case one we want to check and return True if all the three strings have numeric values in them and in case two we want to return True if any of the three strings have numeric values. See the example below of how we approach this problem using python any() function.

# string one
string1 = "3444543"

# string two
string2 = "#$#%))"

# string three
string3 = "%#$sdfadfgfg"

# checking for letter
letter1 = [char.isdigit() for char in string1]
letter2 = [char.isdigit() for char in string2]
letter3 = [char.isdigit() for char in string3]

# checking using multiple conditions
if any(letter1) and any(letter2) and any(letter3):
   print("All have numeric values")
else:
   print("Not all of them have numeric values")

Output:

Not all of them have numeric values

Notice that the else statement is executed because all the conditions in if statement are not true. Now let us use OR logic operator to know if any of them has digits or not. See the example below:

# string one
string1 = "3444543"

# string two
string2 = "#$#%))"

# string three
string3 = "%#$sdfadfgfg"

# checking for letter
letter1 = [char.isdigit() for char in string1]
letter2 = [char.isdigit() for char in string2]
letter3 = [char.isdigit() for char in string3]

# checking using multiple conditions
if any(letter1) or any(letter2) or any(letter3):
   print("At least one of them has numeric value")
else:
   print("Not all of them have numeric values")

Output:

At least one of them has numeric value

 

Summary

Python any() function is a built-in function that works very similarly to OR logic gate. In this tutorial, we covered the usage of any() function taking various scenarios. We learn how any() function works by taking examples of lists, tuples, dictionaries, and strings. At the same time, we learned how we can check if a string has letters or numeric values in it using any() function. Moreover, we also covered how we can use any() function to combine multiple conditions. In a nutshell, this tutorial contains a full description and explanation of python any() function along with various examples.

 

Further Reading

Python any() function
python built-in functions
any() function

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment