I need to compare two lists to see if a sub-list's elements are in another list in the order of the sub-list.
I wrote this code and it does the job exactly:
def ordered_compare(sub_list, matching_list):
# Return true if the items in sub_list and in matching_list in order
if len(matching_list) < len(sub_list):
return False
elif len(matching_list) == 0 or len(sub_list) == 0:
return False
match = None
for matching_list_index in range(len(matching_list) - len(sub_list) + 1):
match = False
for sub_list_index in range(len(sub_list)):
if matching_list[matching_list_index + sub_list_index] == sub_list[sub_list_index]:
match = True
continue
else:
match = False
break
if match: break
return match
What I am wondering though, as I'm learning, is there a more Pythonic way to do this?