6
How the functions work any
and all
in Python and what are the differences between them?
They happen to be functions equivalent to Array.prototype.some
and Array.prototype.every
javascript?
6
How the functions work any
and all
in Python and what are the differences between them?
They happen to be functions equivalent to Array.prototype.some
and Array.prototype.every
javascript?
10
I don’t know enough about the fundamentals of Javascript - but superficially yes.
The "any" and "all" in Python are functions that receive a sequence or iterator and check the truth condition for each element: once the first element is true, any
returns True
- if not, traverse the sequence to the end and return False
.
all
does the opposite - returns False
in the first element with false value (in Python, any sequence or empty Mapping, None, numeric value 0, or a class that implements a method __bool__
custom that returns False
) - and returns True
only if all elements have true value.
They can be used directly with a list, iterable or dictionary (in the case of dictionaries, keys are evaluated), but the most common is that they are used with a "Generator Expression" - that is, an expression with a for
inline to perform some operation for each element of a sequence.
all(chr.isdigit() for chr in "313213882")
for example will return True
The expression preceding the for
is equivalent to the body of a callback function, mandatory in languages that do not have the syntactic construction of Generator Expressions. In Python, if an expression with operators starts to get complicated, just make a function call at the same point (the given example calls a method). One can define a callback function with lambda ali, but it would be redundant - and would have the function overhead be recreated for each element of the sequence:
all((lambda chr: chr.isdigit())(chr) for chr in "313213882")
- wrong shape! :-)
Taking the opportunity to clarify the difference between "Generator Expression" and "list comprehension":
They are similar - "list comprehension" is when we use the same expression type between brackets ([
and ]
) - ai for is executed immediately, and the end result is a list. There are also "set comprehension", which use the same syntax and "Dict comprehension" - almost the same syntax, but have to have two expressions separated by ":" - one for the key, another for the dictionary value.
When you limit the expression with parentheses, or, depending on the use, it is not limited (for example, in a variable assignment) - the correct term is "Generator Expression" - because the end result is a "Generator": an object that implements the Python iterable protocol, and will generate a single element each time it is iterated - that is, it is called its internal method __next__
, be explicitly, with the function next
, or implicitly, in a for
.
For example, in the call to any
and all
, We don’t need extra parentheses or brackets: these functions receive an eternal and consume it (not necessarily all of it) - they are generators. Using the brackets in the call (and therefore a list comprehension) will give the same end result, but Python will generate all the elements of the list (and therefore "solve" the expression between brackets) and call the function: all([chr.isdigit() for chr in "313213882"])
. Don’t do this - in addition to potentially running the expression for unnecessary elements, you’re allocating the memory for all the elements of the sequence at once, when you probably only need one at a time.
Browser other questions tagged python function
You are not signed in. Login or sign up in order to post.
Generator Expression? I thought the name was List comphreension
– Wallace Maxters
I took the opportunity to clarify the difference between list-comprehension and Generator-Expressions.
– jsbueno