TransWikia.com

How iterate over a list and delete values along the way

Stack Overflow Asked by JADEN LISTRANI-AYERS on November 30, 2020

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])

One Answer

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
                

Answered by ubaid shaikh on November 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP