Python any() Function

any() is a built-in function in python programming language which gives a True in return if all elements of an iterable are true (exists) and give False if iterable is empty. All sequence types in Python are examples of iterable. Example sequence types in Python:

  1. Lists
  2. String
  3. Tuple

any() function is a part of Python Built-in Functions (check list)

any() Syntax

any( iterable )

 

any() Parameters

iterable – where argument iterable is an iterable or variable representing iterable (Lists , strig, tuple or custom)

 

any() Return Value

any() function returns a true if elements of iterable are true and return false if iterable is empty.

 

any() Compatibility

This function is available and compatible with both Python 2.x and 3.x

Python 2.x Python 3.x
Yes Yes

 

any() Function Examples

 

When iterable is list (Number) in any()

Example

MyList = [1,2,3,4,5,6]
print (any(MyList))

Output: True

 

When iterable is list (String) in any()

Example

MyStringList = ['a', 'b' , 'c']
print(any(MyStringList))

Output: True

 

Empty list iterable in any()

Example

MyEmptyList = ['a', 'b' , 'c']
print(any(MyEmptyList))

Output: False

 

When iterable is string in any()

Example

print(any("MyString"))

Output: True

 

When iterable is tuple (Number) in any()

Example

MyNumberTuple = (1,2,3,4,5)
print (any(MyNumberTuple))

Output: True

 

When iterable is tuple (string) in any()

Example

MyStringTuple = ('a' , 'b' , 'c')
print (any(MyStringTuple))

Output: True

 

Empty tuple in any()

Example

MyEmptyTuple = ()
print(any(MyEmptyTuple))

Output: False