Open In App

Any All in Python

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Any and All are two built-in functions provided in Python used for successive And/Or. In this article, we will see about any and all in Python.

What is Any in Python

Any Returns true if any of the items is True and returns False if empty or all are false. Any can be thought of as a sequence of OR operations on the provided iterables. It short circuit the execution i.e. stop the execution as soon as the result is known.

Python any() Function Syntax

Syntax: any(list of iterables)

In the below example, we are showing any() function’s working and demonstration by using the below code.

Python3




# Since all are false, false is returned
print (any([False, False, False, False]))
 
# Here the method will short-circuit at the
# second item (True) and will return True.
print (any([False, True, False, False]))
 
# Here the method will short-circuit at the
# first (True) and will return True.
print (any([True, False, False, False]))


Output

False
True
True



In this another example, we are given two lists and we are seeing if any of the number in the list is divisible by 5 by using any function.

Python3




# This code explains how can we
# use 'any' function on list
list1 = []
list2 = []
 
# Index ranges from 1 to 10 to multiply
for i in range(1,11):
    list1.append(4*i)
 
# Index to access the list2 is from 0 to 9
for i in range(0,10):
    list2.append(list1[i]%5==0)
 
print('See whether at least one number is divisible by 5 in list 1=>')
print(any(list2))


Output

See whether at least one number is divisible by 5 in list 1=>
True



What is Python all() function

All Returns true if all of the items are True (or if the iterable is empty). All can be thought of as a sequence of AND operations on the provided iterables. It also short circuit the execution i.e. stop the execution as soon as the result is known. 

Python all() Function Syntax

Syntax: all(list of iterables)

In the below example, we are showing the demonstration and working of the all() function with the help of code below.

Python3




# Here all the iterables are True so all
# will return True and the same will be printed
print (all([True, True, True, True]))
 
# Here the method will short-circuit at the
# first item (False) and will return False.
print (all([False, True, True, False]))
 
# This statement will return False, as no
# True is found in the iterables
print (all([False, False, False]))


Output

True
False
False



In this another example, we are seeing of all numbers in list1 are odd and by using all() function, if they are odd then we will return True otherwise False.

Python3




# Illustration of 'all' function in python 3
# Take two lists
list1=[]
list2=[]
 
# All numbers in list1 are in form: 4*i-3
for i in range(1,21):
    list1.append(4*i-3)
 
# list2 stores info of odd numbers in list1
for i in range(0,20):
    list2.append(list1[i]%2==1)
 
print('See whether all numbers in list1 are odd =>')
print(all(list2))


Output

See whether all numbers in list1 are odd =>
True



Truth table



Similar Reads

Python - Test if all rows contain any common element with other Matrix
Given two Matrices, check if all rows contain at least one element common with the same index row of other Matrix. Input : test_list1 = [[5, 6, 1], [2, 4], [9, 3, 5]], test_list2 = [[9, 1, 2], [9, 8, 2], [3, 7, 10]] Output : True Explanation : 1, 2 and 3 are common elements in rows. Input : test_list1 = [[5, 6, 1], [2, 4], [9, 2, 6]], test_list2 =
5 min read
Python program to check if any key has all the given list elements
Given a dictionary with list values and a list, the task is to write a Python program to check if any key has all the list elements. Examples: Input : test_dict = {'Gfg' : [5, 3, 1, 6, 4], 'is' : [8, 2, 1, 6, 4], 'best' : [1, 2, 7, 3, 9], 'for' : [5, 2, 7, 8, 4, 1], 'all' : [8, 5, 3, 1, 2]}, find_list = [7, 9, 2] Output : True Explanation : best ha
7 min read
How to Fix “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()”
If we work with NumPy or Pandas in Python, we might come across the ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() error. In this article, we will the reason as well as the solution to fix this error. ValueError: The truth value of an array with more than one element is ambiguousIn Python, bo
6 min read
Print first m multiples of n without using any loop in Python
Given n and m, print first m multiples of a m number without using any loops in Python. Examples: Input : n = 2, m = 3 Output : 2 4 6 Input : n = 3, m = 4 Output : 3 6 9 12 We can use range() function in Python to store the multiples in a range. First we store the numbers till m multiples using range() function in an array, and then print the array
3 min read
Python | Sort Tuples in Increasing Order by any key
Given a tuple, sort the list of tuples in increasing order by any key in tuple. Examples: Input : tuple = [(2, 5), (1, 2), (4, 4), (2, 3)] m = 0 Output : [(1, 2), (2, 3), (2, 5), (4, 4)] Explanation: Sorted using the 0th index key. Input : [(23, 45, 20), (25, 44, 39), (89, 40, 23)] m = 2 Output : Sorted: [(23, 45, 20), (89, 40, 23), (25, 44, 39)] E
3 min read
Python program display any message on heart
This article focuses on discussing how to draw a heart with a beautiful message to your loved ones to express what you feel about them. Three types of variants of the above problem statement will be discussed here: Red Heart with a message.Pink-Red Heart with a message.Pink Heart with a message in a different font and color than in the above two ca
3 min read
Python - Test if String contains any Uppercase character
Given a String, Test if it contains any uppercase character. Input : test_str = 'geeksforgeeks' Output : False Explanation : No uppercase character in String.Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String. Method #1 : Using loop + isupper() In this, we iterate for each character in String, check for uppercas
6 min read
Python | Pandas Series/Dataframe.any()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas any() method is applicable both on Series and Dataframe. It checks whether any value in the caller object (Dataframe or series) i
3 min read
Python | Pandas Index.any()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.any() function checks if any of the elements in the index are true or not. It returns one single boolean value if axis is n
2 min read
Python | Remove tuple from list of tuples if not containing any character
Given a list of tuples, the task is to remove all those tuples which do not contain any character value. Example: Input: [(', ', 12), ('...', 55), ('-Geek', 115), ('Geeksfor', 115),] Output: [('-Geek', 115), ('Geeksfor', 115)] Method #1 : Using list comprehension C/C++ Code # Python code to remove all those # elements from list of tuple # which doe
4 min read