[python] Check the elements of the list all, any

When you want to check each element of the list

It is used when you want to check that all the elements in the list have values. For example, when you first initialize the list as [[], [], []], do some processing, and then check that all the elements have values.

all(iterable) Built-in function all This function returns True if all the elements of iterable are true True is other than 0 for numbers, other than'' for letters, other than [] for lists, etc ... So what I'm doing inside is probably like this

all.py


def all(iterable):
    for x in iterable:
       if not x: return False
    else: return True

any(iterable) Built-in function any This function returns True if even one element of iterable is true What I'm doing inside

any.py


def any(iterable):
    for x in iterable:
       if x: return True
    else: return False

Simple example

easy_example.


>>> a = [1,2,3,4,5]
>>> all(a)
True
>>> any(a)
True
>>> a = [0,1,0,0,0]
>>> all(a)
False
>>> any(a)
True
>>> a = [0,0,0,0,0]
>>> all(a)
False
>>> any(a)
False

Summary

all (): Returns True if each element of iterable is true. any (): Returns True if at least one element of iterable is true.

Recommended Posts

[python] Check the elements of the list all, any
[Python] Outputs all combinations of elements in the list
About the basics list of Python basics
Make sure all the elements in the list are the same in Python
Get the number of specific elements in a python list
How to check in Python if one of the elements of a list is in another list
Check the behavior of destructor in Python
Check the existence of the file with python
Check the path of the Python imported module
[Python] Combine all the elements in the array
[Python] Display only the elements of the list side by side [Vertical, horizontal]
List of python modules
[Python] Sort the list of pathlib.Path in natural sort
the zen of Python
Make a copy of the list in Python
Check the date of the flag duty with Python
Easy way to check the source of Python modules
[python] Get the list of classes defined in the module
[Python] Manipulation of elements in list (array) [Add / Delete]
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
[Maya Python] Crush the contents of the script 2 ~ list Notes
Group by consecutive elements of a list in Python
[Python] Get the number of views of all posted articles
Check the asymptotic nature of the probability distribution in Python
Towards the retirement of Python2
Summary of Python3 list operations
Python --Check type of values
About the ease of Python
Check OpenSSL version of python 2.6
[Python] Copy of multidimensional list
Getting list elements in Python
About the features of Python
[Python] Check the installed libraries
The Power of Pandas: Python
Check the operation of Python for .NET in each environment
[Maya Python] Crush the contents of the script 3 ~ List unknown Plugins
Sort and output the elements in the list as elements and multiples in Python.
Check the scope of local variables with the Python locals function.
2015-11-26 python> Display the function list of the module> import math> dir (math)
[Python] Check the current directory, move the directory
The story of Python and the story of NaN
Check the behavior when assigning Python
[Python] The stumbling block of import
[Python] Let's master all and any
First Python 3 ~ The beginning of repetition ~
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Delete multiple elements in python list
Learn the basics of Python ① Beginners
[Python] Let's reduce the number of elements in the result in set operations
How to check the memory size of a variable in Python
[Python of Hikari-] Chapter 05-03 Control syntax (for statement-extracting elements from list-)
How to check the memory size of a dictionary in Python
[python] Get the rank of the values in List in ascending / descending order
Determine if all list elements are present in the dict key
python note: map -do the same for each element of the list
List of disaster dispatches from the Sapporo City Fire Department [Python]