Python all any built-in function

I have been using Python for a long time, still learning lot of simple and new things. Recently I came to know about two built-in functions named all and any. Today while I was solving a hackerrank problem, I used both functions which makes the code a bit nicer, in my opinion.

The functions compares two singly linked lists and if they are equal, returns 1, otherwise returns 0. Here is my code -

def compare_lists(llist1, llist2):
    while all([llist1, llist2]):
        if llist1.data != llist2.data:
            return 0
        llist1 = llist1.next
        llist2 = llist2.next
        
    if any([llist1, llist2]):
        return 0
    
    return 1

Sometimes, this kind of simple thing brings joy to me. Let me know your thoughts. :)

Comments

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code