mepstrip = ['AE38', 'AL29', 'AL30', 'AL35', 'AL41', 'BA37', 'BB37', 'BC37', 'CH24', 'CO26', 'GD29', 'GD30', 'GD35', 'GD38', 'GD41', 'GD46', 'PM29', 'SA24', 'T2V1', 'TDJ3']
indexlist = ['AE38', 'AE38C', 'AE38D', 'AL29', 'AL29C', 'AL29D', 'AL30', 'AL30C', 'AL30D', 'AL35', 'AL35C', 'AL35D', 'AL41', 'AL41C', 'AL41D', 'AY24C', 'BA37D', 'BA37E', 'BAY23', 'BB37D', 'BB37E', 'BC37D', 'BDC24', 'BDC28', 'CEDI', 'CH24D', 'CO23', 'CO26', 'CO26D', 'CUAP', 'DICP', 'DIP0', 'FORM3', 'GD29', 'GD29C', 'GD29D', 'GD30', 'GD30C', 'GD30D', 'GD35', 'GD35C', 'GD35D', 'GD38', 'GD38C', 'GD38D', 'GD41', 'GD41C', 'GD41D', 'GD46', 'GD46C', 'GD46D', 'PAP0', 'PARP', 'PBA25', 'PBY22', 'PM29D', 'PMM29', 'PMY24', 'PR13', 'PUL26', 'SA24D', 'SARH', 'SFA23', 'T2V1C', 'T2V1D', 'T2V2', 'T2V3', 'T2X3', 'T2X4', 'TB23P', 'TB24', 'TC23', 'TC25P', 'TDF24', 'TDJ23', 'TDJ3D', 'TDL23', 'TDS23', 'TFU27', 'TO23', 'TO26', 'TV23', 'TV24', 'TX22', 'TX23', 'TX24', 'TX25', 'TX26', 'TX28', 'TY22P', 'TY27P']
for i in mepstrip:
    if i not in indexlist:
        mepstrip.remove(i)
# ['BB37', 'CH24', 'SA24', 'TDJ3'] remains in mepstrip after for loop despite not in indexlist
            Asked
            
        
        
            Active
            
        
            Viewed 31 times
        
    0
            
            
        - 
                    2A friend just told me not to modify an iterable while iterating it – Juje Oct 22 '22 at 18:42
1 Answers
0
            
            
        You can't remove items from a list while iterating over it. Instead, you could make a copy of the list. And remove the items from that.
new_mepstrip = mepstrip.copy()
for i in mepstrip:
    if i not in indexlist:
        new_mepstrip.remove(i)
 
    
    
        Marco Breemhaar
        
- 168
- 7
 
    