Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)

I'm trying to understand how the any() and all() Python built-in functions work.

I'm trying to compare the tuples so that if any value is different then it will return True and if they are all the same it will return False. How are they working in this case to return [False, False, False]?

d is a defaultdict(list).

print d['Drd2'] 

# [[1, 5, 0], [1, 6, 0]] 

print list(zip(*d['Drd2'])) 

# [(1, 1), (5, 6), (0, 0)] 

print [any(x) and not all(x) for x in zip(*d['Drd2'])] 

# [False, False, False]

To my knowledge, this should output

# [False, True, False]

since (1,1) are the same, (5,6) are different, and (0,0) are the same.

Why is it evaluating to False for all tuples?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code see how Python’s function works:-

def any(iterable): 

   for item in iterable: 

      if item: 

        return True 

   return False 

def all(iterable): 

    for item in iterable: 

      if not item: 

        return False 

    return True

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Nov 22, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Jul 12, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 8, 2019 in Python by Sammy (47.6k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.7k users

Browse Categories