# Python built-in function – any () function with example

Python any() function is another Python built-in function.  any() function return true if any element in the iterable is true, iterable could be list, tuple, dictionary etc.

any function is unlike Python all() function where output is false if any element in iterable is false.

## Syntax of Python any function

Syntax of Python any() function is

any(iterable)

conclusion

• Here any() is a function name.
• Python any function is surrounded by round brackets ().
• any function take iterable as parameter.
• iterable could be list, tuple, set, dictionary etc.
• any function in Python will return True if at least one element is True or Non-zero.

## Examples of Python any() function

Let’s take some example to understand how any function works in Python.

below is example of Python any() function with Python list

### Python list with any() function

#Python any() function with list
myList=[True,False,False,False]
myList2=[False,False,False,False]
print("any function with myList:",any(myList))
print("any function with myList2:",any(myList2))

Output

any function with myList: True
any function with myList2: False

Explanation

• Here myList and myList2 are two python lists.
• In myList, there is one True element, that’s why in output, any function return True.
• In myList2, there are all False element, that’s why in output, any function return False.

Let’s take few examples with Python tuple, dictionary, set etc.

### Python tuple with any() function

#Python any() function with Python tuple

first=(1,3,4,5)
second=[0,0,0,0,1]
third=[0,0,0,0,0]

print(type(first))
print("any function with first:",any(first))
print("any function with second:",any(second))
print("any function with third:",any(third))

READ  Python ascii() function with example

Output

<class 'tuple'>
any function with first: True
any function with second: True
any function with third: False

Explanation

• Here first, second, third are three Python tuples.
• Output of first and second tuples are True, as at least  one element is either True or non-zero.
• Output of third Python tuple is false as all elements of third tuple are 0.

### Python set with any() function

#Python any() function with Python set

first={1,3,4,5}
second={0,0,0,0,1}
third={0,0,0,0,0}

print(type(first),type(second),type(third))

print(first,second,third)

print("any function with first:",any(first))
print("any function with second:",any(second))
print("any function with third:",any(third))

Output

<class 'set'> <class 'set'> <class 'set'>
{1, 3, 4, 5} {0, 1} {0}
any function with first: True
any function with second: True
any function with third: False

Explanation

• Here first, second, third are three Python sets.
• Python set, does not contain duplicate elements, that’s why it removes 0’s from second and third Python set.
• Output of first and second Python set is True as they contain at least one True elements or non-zero element.

### Python dictionary with any()

#Python any() function with Python dictionary

first={1:"one",
2:"two",
3:"three"
}

second={0:"zero",
1:"one"
}

third={0:"zero"}

print(type(first),type(second),type(third))

print(first,second,third)

print("any function with first:",any(first))
print("any function with second:",any(second))
print("any function with third:",any(third))

Output

<class 'dict'> <class 'dict'> <class 'dict'>
{1: 'one', 2: 'two', 3: 'three'} {0: 'zero', 1: 'one'} {0: 'zero'}
any function with first: True
any function with second: True
any function with third: False

Explanation

• Here first, second, third are three Python dictionary.
• Output of first and second Python dictionary is True as at least one key in key-value pair is True or non zero.
• Output of third Python dictionary is False, as key in key-value pair is 0.
READ  Use of Python hex() function with example