To compare two lists of lists (or lists of tuples, as you've mentioned) in Python where the inner lists' order matters but the outer list's order does not, you can approach the problem by sorting the outer list based on sorted inner lists. This way, the order of the inner lists does not affect the comparison, but the order within the inner lists does.
Here's a straightforward method to achieve this:
python
def compare_lists_of_lists(list1, list2):
# Sorting the outer list by sorting each inner list and then comparing
sorted_list1 = sorted(list1, key=lambda x: tuple(sorted(x)))
sorted_list2 = sorted(list2, key=lambda x: tuple(sorted(x)))
return sorted_list1 == sorted_list2
# Example usage:
list1 = [[1, 2], [3, 4]]
list2 = [[3, 4], [1, 2]]
print(compare_lists_of_lists(list1, list2)) # Output: True
list3 = [[1, 2], [3, 4, 5]]
list4 = [[3, 4], [1, 2, 5]]
print(compare_lists_of_lists(list3, list4)) # Output: False, because one list has an element more than the other in an inner list
list5 = [[1, 2], [3, 4]]
list6 = [[1, 2], [3, 4]]
print(compare_lists_of_lists(list5, list6)) # Output: True
However, if your inner lists are actually tuples (as initially mentioned), and you want to preserve that they are tuples, you can modify the approach slightly:
In both cases, the sorted function is used with a key argument that specifies how to sort the elements. For lists of lists or lists of tuples, sorting based on the sorted version of each inner list/tuple ensures that the order within the inner lists/tuples is considered but the order of the outer list is ignored.
This approach assumes that the elements within the inner lists/tuples are comparable (which is the case for numbers, strings, etc., but not necessarily for all types of objects). If your lists/tuples contain uncomparable objects, a different approach might be needed.