• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Python any() Function

Python any() Function

Author: Aditya Raj
Last Updated: May 18, 2022

In python, we normally use the comparison operators and the logical operators to check for conditions for a different number of elements. What if you have to check for a condition in a list of elements? In this article, we will discuss the any() function in python. We will also see how we can use the any() function with different iterable objects.

Table of Contents
  • What is any() in Python?
  • The any() Function With Iterable Objects in Python
  • The any() Function With Dictionaries in Python
  • Conclusion

What is any() in Python?

The any() function is used to check if there exists an element in an iterable object that evaluates to True or not. The any() function takes an iterable object like a list, tuple, set, dictionary, or string as its input argument. After execution, it returns True if at least one element of the iterable evaluates to True. Otherwise, it returns False. You can observe this in the following example.

myList1 = [1, 2, 3, 4]
myList2 = [False, False, False]
myList3 = []
print("The list is:", myList1)
output = any(myList1)
print("List contains one or more  elements that evaluate to True:", output)
print("The list is:", myList2)
output = any(myList2)
print("List contains one or more  elements that evaluate to True:", output)
print("The list is:", myList3)
output = any(myList3)
print("List contains one or more  elements that evaluate to True:", output)

Output:

The list is: [1, 2, 3, 4]
List contains one or more  elements that evaluate to True: True
The list is: [False, False, False]
List contains one or more  elements that evaluate to True: False
The list is: []
List contains one or more  elements that evaluate to True: False

You can understand the working of the any() function as an application of the or operator. For an iterable object having elements element1, element2, element3,.... elementN, using the any() is equivalent to executing the statement element1 OR element2 OR element3 OR ….., OR elementN.

The any() Function With Iterable Objects in Python

When we pass a list as an input argument to the any() function, it returns True if at least one of the elements of the list evaluates to True.

myList1 = [1, 2, 3, 4]
myList2 = [False, False, False]
myList3 = []
print("The list is:", myList1)
output = any(myList1)
print("List contains one or more  elements that evaluate to True:", output)
print("The list is:", myList2)
output = any(myList2)
print("List contains one or more  elements that evaluate to True:", output)
print("The list is:", myList3)
output = any(myList3)
print("List contains one or more  elements that evaluate to True:", output)

Output:

The list is: [1, 2, 3, 4]
List contains one or more  elements that evaluate to True: True
The list is: [False, False, False]
List contains one or more  elements that evaluate to True: False
The list is: []
List contains one or more  elements that evaluate to True: False

When we pass an empty list to the any() function, it returns False.

When we pass any non-empty string as an input argument to the any() function, it returns True.

myStr1="PythonForBeginners"
myStr2=""
print("The string is:", myStr1)
output = any(myStr1)
print("The string contains one or more  elements:", output)
print("The string is:", myStr2)
output = any(myStr2)
print("The string contains one or more  elements:", output)

Output:

The string is: PythonForBeginners
The string contains one or more  elements: True
The string is: 
The string contains one or more  elements: False

For an empty string, the any() function returns False.

Similar to lists, when we pass a set as an input argument to the any() function, it returns True if at least one of the elements of the set evaluates to True.

mySet1 = {1, 2, 3, 4}
mySet2 = {False}
mySet3 = set()
print("The Set is:", mySet1)
output = any(mySet1)
print("Set contains one or more  elements that evaluate to True:", output)
print("The Set is:", mySet2)
output = any(mySet2)
print("Set contains one or more  elements that evaluate to True:", output)
print("The Set is:", mySet3)
output = any(mySet3)
print("Set contains one or more  elements that evaluate to True:", output)

Output:

The Set is: {1, 2, 3, 4}
Set contains one or more  elements that evaluate to True: True
The Set is: {False}
Set contains one or more  elements that evaluate to True: False
The Set is: set()
Set contains one or more  elements that evaluate to True: False

When we pass an empty set to the any() function, it returns False.

The any() Function With Dictionaries in Python

When we pass a dictionary to the any() function as the input argument, it returns True if at least one key of the python dictionary evaluates to True. Otherwise, it returns False.

myDict1 = {1: 1, 2: 2, 3: 3, 4: 4}
myDict2 = {False: 1}
myDict3 = {}
print("The Dictionary is:", myDict1)
output = any(myDict1)
print("Dictionary contains one or more  keys that evaluate to True:", output)
print("The Dictionary is:", myDict2)
output = any(myDict2)
print("Dictionary contains one or more  keys that evaluate to True:", output)
print("The Dictionary is:", myDict3)
output = any(myDict3)
print("Dictionary contains one or more  keys that evaluate to True:", output)

Output:

The Dictionary is: {1: 1, 2: 2, 3: 3, 4: 4}
Dictionary contains one or more  keys that evaluate to True: True
The Dictionary is: {False: 1}
Dictionary contains one or more  keys that evaluate to True: False
The Dictionary is: {}
Dictionary contains one or more  keys that evaluate to True: False

When we pass an empty dictionary to the any() function, it returns False.

Conclusion

In this article, we have discussed any() function in python. We also used the any() function with different iterable objects and observed the outputs of the function. To learn more about python programming, you can read this article on list comprehension.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us