Check Whether Two Lists are Circularly Identical
Last Updated :
18 Jan, 2025
We are given two lists of elements, and the task is to check if one list is a circular rotation of the other.
For example, the lists [10, 10, 10, 0, 0] and [10, 10, 10, 0, 0] are circularly identical because the second list can be obtained by rotating the first list. Let's discuss various ways to perform this check in Python.

Using Concatenation
We can concatenate the first list with itself and check if the second list is a sublist of this concatenated list. This is a straightforward and efficient method.
Python
a = [10, 10, 10, 0, 0]
b = [10, 10, 10, 0, 0]
print(str(b)[1:-1] in str(a + a)[1:-1])
print("GFG")
Explanation:
- The first list is concatenated with itself to simulate all possible rotations.
- Both lists are converted to strings for direct comparison.
- This method is efficient and works well for circular rotations.
Let's explore some more ways and see how we can check whether two lists are circularly identical or not.
Using deque rotations
The deque structure from collections allows efficient rotation of lists, which can then be compared.
Python
from collections import deque
a = deque([10, 10, 10, 0, 0])
b = [10, 10, 10, 0, 0]
c = False
for _ in range(len(a)):
if list(a) == b:
c = True
break
a.rotate(1)
print(c)
Explanation:
- deque is rotated to simulate circular shifts.
- rotated deque is converted to a list for comparison with
b. - method is less concise but useful for clarity.
Element shifting with modulus
We can use the modulus operator to simulate circular shifting without explicitly rotating the list.
Python
a = [10, 10, 10, 0, 0]
b = [10, 10, 10, 0, 0]
n = len(a)
c = False
for i in range(n):
if all(a[(j + i) % n] == b[j] for j in range(n)):
c = True
break
print(c)
Explanation:
- Each element in
a is matched to the corresponding rotated position in b using the modulus operator. - This avoids creating new lists or strings, making it memory-efficient.
- This method is efficient and handles circular rotations directly.
Comparing sets of rotations
We can generate all rotations of one list and compare it with the other.
Python
a = [10, 10, 10, 0, 0]
b = [10, 10, 10, 0, 0]
rotations = [a[i:] + a[:i] for i in range(len(a))]
print(b in rotations)
Explanation:
- All possible rotations of
a are generated and stored in a list. - The list
b is checked for membership in this list of rotations. - This method is clear but involves extra space for storing rotations.
Using for loop
We can manually rotate one list and check if it matches the other.
Python
a = [10, 10, 10, 0, 0]
b = [10, 10, 10, 0, 0]
n = len(a)
c = False
for i in range(n):
if a[i:] + a[:i] == b:
c = True
break
print(c)
Explanation:
- list
a is manually rotated by slicing and compared with b. - The process stops as soon as a match is found.
- This method is easy to implement but less efficient for longer lists.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice