Hi! I have a similar problem, you can see the original post here: Python list comparison - #5 by akselalvarez
It seems that I don’t manage to feed the list into the script, since what I get is just two empty lists as result, but they preserve the same structure as my start list, therefore I think something may work.
I have a list that has several branches, each branch has 3 indices, I want to compare each branch so the values in the indices 1 and 2 are checked, if the two of them are repeated in another branch, then the complete branch will be placed in a list called “branches_duplicate”. If they don’t or are the first occurrence, they are to be placed in a list called “branches_unique”.
Until now it seems that I can print two lists, but they seem to be empty.
Here is my code:
# Provided list of branches
branches = [] # List of values from Model
branches_unique = [] # List to store branches with unique values at indices 1 and 2
branches_duplicate = [] # List to store branches with repeating values at indices 1 and 2
# Iterate through each branch
for branch in branches:
seen_values = set() # Keep track of unique values encountered in the current branch
duplicate = False # Flag to indicate if the branch has duplicate values
# Check each element in the branch
for i in range(1, 3): # Check indices 1 and 2
if branch[i] in seen_values:
duplicate = True
break
else:
seen_values.add(branch[i])
# Append the branch to the appropriate list
if duplicate:
branches_duplicate.append(branch)
else:
branches_unique.append(branch)
print(branches_unique)
print(branches_duplicate)