×

any() Keyword in python

“any” keyword in python

This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is?

Python any () function is used to perform operations on iterable. Here, iterables can be Lists, dictionaries, Tuples, sets, etc...

It returns true if there are any true values in the iterables else, it will return false.

It might be a bit difficult to understand, but you can understand it clearly with some examples.

Syntax:

any(iterables)

Iterable parameter: This can be any iterable object like List, Tuple, Set, Dictionary etc.

Working of any () Function with LISTS

Example:

lst = [1, 4, 3, 4, 4, 5, 1]
# All values in the list are true values.
print(any(lst))
lst = [False, 0, 0, False]
# All values in the list are true.
print(any( lst ))
lst= [ 6, 4, 5, 1, 0, 6, 7, False]
# Some elements in the list are true, while other values are false.
print(any( lst ))
l = []
# List is empty.
print(any( l ))

Output

“any” keyword in python

From the above code, you can see the following:

  1. Lst= [1, 4,3,4,4,5,1] when you perform any () function on this list, you will get True because the list contains true iterable values. Hence you will get the output as TRUE.
  2. Lst = [False, 0, 0, False] when you perform any () function on this list, you will get False because the list contains only false values (False, 0). Hence, you will get the output as FALSE.
  3. When you perform any () function on an iterable with the combination of both true values and false values, you will output True.

Ex: [6, 4, 5, 1, 0, 6, 7, False]

  • If you perform any () function on an empty iterable object, then you will output false because the null value is treated as false.

Working of any () Function with TUPLES.

Example:

t = (1, 4, 3, 4, 4, 5, 1)
# All values in the tuple are true values.
print(any(t))
t = (False, 0, 0, False)
# All elements in the tuple are false values.
print(any( t ))
t= (6, 4, 5, 1, 0, 6, 7, False)
# Some elements in the tuple are true, while other values are false.
print(any( t ))
T = ()
# Tuple is empty.
print(any( l ))

Output

“any” keyword in python

Working of any () Function with SETS.

Example:

Set = {1, 4, 3, 4, 4, 5, 1}
# All elements in the set are true values.
print (any (Set))
Set = {False, 0, 0, False}
# All elements in the set are false values.
print (any (Set))
Set= {6, 4, 5, 1, 0, 6, 7, False}
# Some elements in the set are true, while other values are false.
print (any (Set))
Set = {}
# Set is empty.
print (any (T))

Working of any () function with Dictionaries.

While working with dictionaries function of any () is a bit different. In a dictionary, if the dictionary is empty or all keys of a dictionary are false, any () function returns false. Otherwise, it will return true.   

Example:

# All elements of dictionary are true
di = {1: "Java", 2: "code”}
print(any(di))
# All elements of dictionary are false
di = {0: "Java", False: "code"}
print(any(di))
# Some elements of dictionary are true while others are false
di = {0: "java",1: "code"}
print(any(di))
# Empty dictionary
di = {}
print(any(di))
“any” keyword in python

Working of any () function with Strings.

Example:

# Non-Empty String
s = "Javaprogram !"
print(any(s))
# Non-Empty String
s = "000"
print(any(s))
# Empty string
s = ""
print(any(s))

“any ()” function can also be used with looping and conditional statements.

You can refer to the below code to understand the usage of the "any ()" function with loops.

Working of any () function with Loops.

Example:

def any(lst):
    for i in list:
        if i:
            return True
    return False
 
x = [6,3,0,4,9,3,9,8]
print(any(x))

If you pass an empty list in the above code, then the output will be False because the null value is considered false and iterating null value outputs false.

This is the usage of any () function in python, where you can check true or false for iterables.


Related Topics

Google Chrome API in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

Idle python download for Windows

Here, we will be discussing how to get the answer to all questions related to installing Python on Windows. What is IDLE? Integrated Development and Learning Environment, or IDLE, is a shorthand....

3 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

Data Distribution in python

Before discussing data distribution, we will discuss each word that is data and Distribution. What is meant by data? A collection of raw facts and figures is called data. The word "raw"...

18 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.