0

I am trying to use list.remove() within a nested for loop in python, however, when I try to remove the items (as per my condition), it skips elements in the list. I have tried using list.copy(), but it has not worked yet.

#get total observations
for iobs,obs in enumerate(observe):

    #get channel for each observation 
    for ichannel, channel in enumerate(channels):

        #get samples for each channel - these are nested dictionaries
        samples = channel['samples']

        #loop over each sample per channel              
        for isamp, samp in enumerate(samples):

            if ('mufakes') in samp['name']:
                continue                    
            if(obs['name']!= channel['name']):
                continue

            #calculations for removal conditions
            Nominal = samp['data']
            Total = obs['data']
            fr = np.divide(Nominal,Total)
            maxfract = np.max(fr)
            
            #copy to a new list 
            _samples = samples.copy()

            if maxfract < 0.02:
                #orginal list is samples, index to remove is isamp              
                samples.remove(_samples[isamp])

I want to remove the index - isamp from the list of samples, for each channel and each observation, in the original list. But when I print out the removed samples, it does not seem to work, it skips over every other element in the list.

Thanks !

Best, Shreya

Shreya
  • 13
  • 2

0 Answers0