I have a list with multiple list objects in it, I wish to compare each inner list to all other inner lists in the outer list object and if a match is found, print it out.
I have already tried looping through each object in the list and compare it to all other objects but I always match the one I start with.
My example list is this:
list_of_lists = [
[1, 11, 17, 21, 33, 34],
[4, 6, 10, 18, 22, 25],
[1, 15, 20, 22, 23, 31],
[3, 5, 7, 18, 23, 27],
[3, 22, 24, 25, 28, 37],
[7, 11, 12, 25, 28, 31],
[1, 11, 17, 21, 33, 34],
...
]
Note that list_of_lists[0]
matches list_of_lists[6]
, which I wish to match in this example.
The expected result is a loop that goes through each list object and compares it to all other objects, if there's a match - print it out.
You could do something like this:
list_of_lists = [
[1, 11, 17, 21, 33, 34],
[4, 6, 10, 18, 22, 25],
[1, 15, 20, 22, 23, 31],
[3, 5, 7, 18, 23, 27],
[3, 22, 24, 25, 28, 37],
[7, 11, 12, 25, 28, 31],
[1, 11, 17, 21, 33, 34],
]
for i in range(len(list_of_lists)):
for j in range(len(list_of_lists)):
# If you're checking the row against itself, skip it.
if i == j:
break
# Otherwise, if two different lists are matching, print it out.
if list_of_lists[i] == list_of_lists[j]:
print(list_of_lists[i])
This outputs:
[1, 11, 17, 21, 33, 34]