0

I'm currently making a match making system of some sort, and I want to iterate over a list and match the people in the list up. But I don't know how to get rid of the values (A and B) as I iterate and not raise the "list.remove(x): x not in list error". Does anyone know I can achieve this ?

for A in SORTEDUsers:
    for B in SORTEDUsers:
        if A[1] == (B[1]-1) or A == (B[1]) or A == (B[1]+1) and (B[2] in A[3]) and A[5] in B[5] and A != B:
            print(A[0],B[0])
  • 3
    Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Brian61354270 Jul 31 '20 at 17:48

1 Answers1

0

How about if you make another array or list which denotes whether a person has been selected or not like below:

selected=[False]*len(SORTEDUsers)
for i in range(len(SORTEDUsers)):
    if not selected[i]:
        A=SORTEDUsers[i]
        for j in range(len(SORTEDUsers)):
            if not selected[j]:
                B=SORTEDUsers[j]
                if A[1] == (B[1]-1) or A == (B[1]) or A == (B[1]+1) and (B[2] in A[3]) and A[5] in B[5] and A != B:
                    print(A[0],B[0])
                    selected[i]=True
                    selected[j]=True
                
halfer
  • 19,824
  • 17
  • 99
  • 186
ubaid shaikh
  • 453
  • 3
  • 7