Python | Check whether two lists are circularly identical - GeeksforGeeks

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

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 notdef 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 codelist1 = [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") 
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 notdef 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 codelist1 = [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") 
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 notdef circularly_identical(list1, list2):         return(' '.join(map(str, list2)) in ' '.join(map(str, list1 * 2)))        # driver codelist1 = [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") 
Yes
No

nikhilk26
circular-array
Python list-programs
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Box Plot in Python using Matplotlib
Bar Plot in Matplotlib
Python | Get dictionary keys as a list
Python | Convert set into a list
Ways to filter Pandas DataFrame by column values
Python - Call function from another file
loops in python
Multithreading in Python | Set 2 (Synchronization)
Python Dictionary keys() method
Python Lambda Functions