

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if tuple has any None value in Python
When it is required to check if a tuple has any 'None' value or not, the 'any' method, the 'map' method and the lambda function can be used.
The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.
Anonymous function is a function which is defined without a name.
In general, functions in Python are defined using 'def' keyword, but anonymous function is defined with the help of 'lambda' keyword. It takes a single expression, but can take any number of arguments. It uses the expression and returns the result of it.
The 'any' method checks to see the iterable if at least one True value exist. If yes, it returns True, else False.
Below is a demonstration of the same −
Example
my_tuple = (31, 45, 12, 56, 78, None, None) print("The tuple is : ") print(my_tuple) my_result = any(map(lambda elem: elem is None, my_tuple)) print("Does the tuple contain any None value ? " ) print(my_result)
Output
The tuple is : (31, 45, 12, 56, 78, None, None) Does the tuple contain any None value ? True
Explanation
- A tuple is defined and is displayed on the console.
- The lambda function is applied on each element in the tuple using 'map' method.
- The any function is called on this result and it is assigned to a variable.
- This variable is displayed on the console.
- Related Questions & Answers
- Check for None Tuple in Python
- How to check if a matrix has any missing value in R?
- How to check if a data frame has any missing value in R?
- Python – Check if any list element is present in Tuple
- Check if variable is tuple in Python
- Python – Test if tuple list has a single element
- Python Pandas - Check if the index has NaNs
- Check if element is present in tuple in Python
- Check if tuple and list are identical in Python
- Python Pandas - Check if the index has duplicate values
- Python Pandas - Check if the index has unique values
- Python Pandas - Check if the IntervalIndex has overlapping intervals
- Check if a Binary Tree (not BST) has duplicate value in C++
- Check if any permutation of N equals any power of K in Python
- Check if any alert exists using selenium with python.
Advertisements