AnswerBun.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!

Related Questions

Replace duplicates items in array with different values

3  Asked on November 7, 2021 by mohamed-eshaftri

         

How to fix object “x” not found

2  Asked on November 7, 2021 by ronie-febriansah

 

link error when using random_device from boost

2  Asked on November 7, 2021 by dikiidog

     

Error: invalid input syntax for type integer

0  Asked on November 7, 2021 by hammad-ali

     

android studio : My app crashes immediately after start

5  Asked on November 7, 2021 by user13930404

   

Int + Str type python

1  Asked on November 7, 2021

     

Javascript Output is giving empty response

0  Asked on November 7, 2021 by manish-jha

     

Create many CSV Files based on Pandas df column value

2  Asked on November 7, 2021 by eithar

       

How to change radio button checked attribute

2  Asked on November 7, 2021 by white-death

       

Vue js/Javascript Objects.assign() not working

0  Asked on November 7, 2021 by madsongr

     

Problems with HTTP/2 on nginx for Windows?

0  Asked on November 7, 2021 by camaross

     

Error to start application after insert Entity Framework

1  Asked on November 7, 2021 by bircastri

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir