How does the python any () function work?

On the python docs page for any equivalent code for the any() function is listed as:

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

How does this function know which element I want to check if you call it in this form?

 any(x > 0 for x in list) 

From the function definition, all I see is that I pass the iterable object. How does the for loop loop know that I was looking for something > 0 ?

+53
python
May 12 '13 at 8:18
source share
5 answers

If you use any(lst) , you see that lst is iterable, which is a list of some elements. If it contains [0, False, '', 0.0, [], {}, None] (all of which have logical values False ), then any(lst) will be False . If lst also contained any of the following [-1, True, "X", 0.00001] (all of which are evaluated before True ), then any(lst) will be True .

In the code you posted, x > 0 for x in lst is another type of iteration called a generator expression . Before generator expressions were added in Python, you would create a list comprehension that looks very similar, but with the surrounding [] 's: [x > 0 for x in lst] . From lst containing [-1, -2, 10, -4, 20] , you get this meaningful list: [False, False, True, False, True] . This internal value will then be passed to the any function, which will return True , since there is at least one True value.

But with a generator expression, Python no longer needs to create this internal list of True(s) and False(s) , the values ​​will be generated as the any function iterates using the generated values ​​at that time according to the generator expression. And, since there are any short circuits, it will stop the iteration as soon as it sees the first True value. This would be especially convenient if you created lst using something like lst = range(-1,int(1e9)) (or xrange if you use Python2.x). Despite the fact that this expression will generate more than a billion records, any should only go to the third record when it reaches 1 , which evaluates True for x>0 , and therefore any can return True .

If you created a list comprehension, Python would first have to create a list of a billion elements in memory, and then pass this to any . But with a generator expression , you can have Python built-in functions, such as any and all , exit early as soon as you see True or False .

+84
May 12 '13 at 8:38
source share
β€” -

(x > 0 for x in list) in this function call, a generator expression is created, for example.

 >>> nums = [1, 2, -1, 9, -5] >>> genexp = (x > 0 for x in nums) >>> for x in genexp: print x True True False True False 

Which any uses, and shortcircuits when meeting the first object that evaluates True

+17
May 12, '13 at 8:21
source share
 >>> names = ['King', 'Queen', 'Joker'] >>> any(n in 'King and john' for n in names) True >>> all(n in 'King and Queen' for n in names) False 

It just cuts a few lines of code into one. You do not need to write long code, for example:

 for n in names: if n in 'King and john': print True else: print False 
+13
Jun 08 '16 at 7:29
source share

This is because iterable

 (x > 0 for x in list) 

Note that x > 0 returns either True or False , and therefore you have Boolean iterability.

+5
May 12 '13 at 8:20
source share

Simply put, any () does the job: according to the condition, even if it encounters one executable value in the list, it returns true, otherwise it returns false.

 list = [2,-3,-4,5,6] a = any(x>0 for x in lst) print a: True list = [2,3,4,5,6,7] a = any(x<0 for x in lst) print a: False 
+1
May 5 '16 at 9:37
source share



All Articles