Related Articles
Python | Check whether two lists are circularly identical
• Last Updated : 11 May, 2020

Given two lists, check if they are circularly identical or not. Examples:

```Input : list1 = [10, 10, 0, 0, 10]
list2 = [10, 10, 10, 0, 0]
Output : Yes
Explanation: yes they are circularly identical as when we write the list1
last index to second last index, then we find it is circularly
same with list1
Input : list1 = [10, 10, 10, 0, 0]
list2 = [1, 10, 10, 0, 0]
Output :No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 : Using List Traversal

Using traversal, we have to double the given list. Check for any x(0<=n) to any x+n and compare with list2 to see if the list1 and list2 are same, if both are same then the list2 is circularly identical. Using two loops, check this property. The first loop will run from 0 to len(list1) and then check if the index (x to x+n) is same as list2, if yes then return true, else return false.

Below is the Python implementation of the above approach:

 `# python program to check if two ``# lists are circularly identical``# using traversal`` ` `# function to check circularly identical or not``def` `circularly_identical(list1, list2):``     ` `    ``# doubling list``    ``list3 ``=` `list1 ``*` `2``     ` `    ``# traversal in twice of list1``    ``for` `x ``in` `range``(``0``, ``len``(list1)):``        ``z ``=` `0` `         ` `        ``# check if list2 == list1 curcularly ``        ``for` `y ``in` `range``(x, x ``+` `len``(list1)):``            ``if` `list2[z]``=``=` `list3[y]:``                ``z``+``=` `1``            ``else``:``                ``break``             ` `        ``# if all n elements are same circularly ``        ``if` `z ``=``=` `len``(list1):``            ``return` `True` `     ` `    ``return` `False``         ` ` ` ` ` `# driver code``list1 ``=` `[``10``, ``10``, ``0``, ``0``, ``10``]``list2 ``=` `[``10``, ``10``, ``10``, ``0``, ``0``]``list3 ``=` `[``1``, ``10``, ``10``, ``0``, ``0``]`` ` ` ` `# check for list 1 and list 2 ``if``(circularly_identical(list1, list2)):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)`` ` `# check for list 2 and list 3 ``if``(circularly_identical(list2, list3)):``    ``print` `(``"Yes"``) ``else``:``    ``print` `(``"No"``) `
Output:
```Yes
No
```

Method 2 : Using List Slicing

 `# python program to check if two ``# lists are circularly identical``# using traversal``  ` `# function to check circularly identical or not``def` `circularly_identical(list1, list2):``  ` `  ``# doubling list``  ``list1.extend(list1)``  ` `  ``# traversal in twice of list1``  ``for` `i ``in` `range``(``len``(list1)):``  ` `    ``# check if sliced list1 is equal to list2``    ``if` `list2 ``=``=` `list1[i: i ``+` `len``(list2)]:``  ` `      ``return` `True``  ``return` `False`` ` ` ` `# driver code``list1 ``=` `[``10``, ``10``, ``0``, ``0``, ``10``]``list2 ``=` `[``10``, ``10``, ``10``, ``0``, ``0``]``list3 ``=` `[``1``, ``10``, ``10``, ``0``, ``0``]`` ` ` ` `# check for list 1 and list 2 ``if``(circularly_identical(list1, list2)):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)`` ` `# check for list 2 and list 3 ``if``(circularly_identical(list2, list3)):``    ``print` `(``"Yes"``) ``else``:``    ``print` `(``"No"``) `
Output:
```Yes
No
```

Method 3 : Using map() function

Using Python’s inbuilt function map() we can do this in one single step, where we have to map list2 in a string and then see if it exists in the mapping of twice of list1 (2*list1) in another string.

Below is the Python implementation of the above approach:

 `# python program to check if two ``# lists are circularly identical``# using map function `` ` `# function to check circularly identical or not``def` `circularly_identical(list1, list2):``    ` `    ``return``(``' '``.join(``map``(``str``, list2)) ``in` `' '``.join(``map``(``str``, list1 ``*` `2``)))``     ` ` ` `# driver code``list1 ``=` `[``10``, ``10``, ``0``, ``0``, ``10``]``list2 ``=` `[``10``, ``10``, ``10``, ``0``, ``0``]``list3 ``=` `[``1``, ``10``, ``10``, ``0``, ``0``]`` ` ` ` `# check for list 1 and list 2 ``if` `(circularly_identical(list1, list2)):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)`` ` `# check for list 2 and list 3 ``if``(circularly_identical(list2, list3)):``    ``print` `(``"Yes"``) ``else``:``    ``print` `(``"No"``) `
Output:
```Yes
No
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up