w3resource

Python: Check whether two lists are circularly identical

Python List: Exercise - 26 with Solution

Write a Python program to check whether two lists are circularly identical.

Visual Presentation

Python: Check whether two lists are circularly identical

Sample Solution:

Python Code:

# Define three lists: list1, list2, and list3, each containing a sequence of numbers
list1 = [10, 10, 0, 0, 10]
list2 = [10, 10, 10, 0, 0]
list3 = [1, 10, 10, 0, 0]

# Compare list1 and list2
print('Compare list1 and list2')

# Check if the string representation of list2 is present in the string representation of list1 repeated twice
# The result will be True if list2 is a subsequence of list1 repeated twice, otherwise False
print(' '.join(map(str, list2)) in ' '.join(map(str, list1 * 2)))

# Compare list1 and list3
print('Compare list1 and list3')

# Check if the string representation of list3 is present in the string representation of list1 repeated twice
# The result will be True if list3 is a subsequence of list1 repeated twice, otherwise False
print(' '.join(map(str, list3)) in ' '.join(map(str, list1 * 2))) 

Sample Output:

Compare list1 and list2                                                                                       
True                                                                                                          
Compare list1 and list3                                                                                       
False 

Flowchart:

Flowchart: Check whether two lists are circularly identical

Python Code Editor:

Previous: Write a Python program to select an item randomly from a list.
Next: Write a Python program to find the second smallest number in a list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/list/python-data-type-list-exercise-26.php