Signup/Sign In

Python | Checking whether two Lists are Circularly Identical

Posted in Programming   LAST UPDATED: SEPTEMBER 27, 2021

    In this tutorial, we will understand how to identify if two lists are circularly identical. But first, let's understand what does it mean, being circularly identical? Let us take an example, below we have some sample input and output.

    Sample input: [1, 2, 1, 1, 3] and [3, 1, 2, 1, 1]

    Sample output: Yes

    Sample input: [3, 1, 2, 1, 1] and [2, 1, 1, 2, 1]

    Sample output: No

    It can be seen that circularly identical refers to two lists that can be obtained from each other if one or more of the elements in the list are rotated/displaced from their original index and placed at the beginning. For example: the list [1,2,1,1,3] element at index 4, i.e the element 3 can be placed at the 0th index. This would give list [3,1,2,1,1]. The elements are merely rotated, the other values order in the list is not lost.

    1. By iterating through the lists

    The naive approach is to traverse over the lists and check if the respective elements are equal. In the code example below, we have done that:

    def circular_identical(list_1, list_2):
        list_3 = list_1 * 2
        for x in range(0, len(list_1)):
            z = 0
            for y in range(x, x + len(list_1)):
                if list_2[z]== list_3[y]:
                    z+= 1
                else:
                    break
            if z == len(list_1):
                return True
        return False
    
    list_1 = [1, 2, 1, 1, 3]
    list_2 = [3, 1, 2, 1, 1]
    list_3 = [2, 1, 1, 2, 1]
    
    print("List one and two are being checked")
    if(circular_identical(list_1, list_2)):
        print("Yes")
    else:
        print("No")
    
    print("List two and three are being checked")
    if(circular_identical(list_2, list_3)):
        print("Yes")
    else:
        print("No")


    List one and two are being checked
    Yes
    List two and three are being checked
    No

    2. Using the Python map function

    Using the map function, we can do this in less code. This is a bit tricky, but its a quick way to verify if two lists are circular identical or not.

    def circular_identical(list_1, list_2):
        return(' '.join(map(str, list_2)) in ' '.join(map(str, list_1 * 2)))
    
    
    list_1 = [1, 2, 1, 1, 3]
    list_2 = [3, 1, 2, 1, 1]
    list_3 = [2, 1, 1, 2, 1]
    
    print("List one and two are being checked")
    if(circular_identical(list_1, list_2)):
        print("Yes")
    else:
        print("No")
    
    print("List two and three are being checked")
    if(circular_identical(list_2, list_3)):
        print("Yes")
    else:
        print("No")


    List one and two are being checked
    Yes
    List two and three are being checked
    No

    Conclusion

    In this post, we saw how two lists can be checked to be circularly identical. Let us know what you think about this post in the comment section below.

    You may also like:

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:PythonPython List
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS