I am getting an error when I use remove method in the following code
<pre><code>def cluster(body1list,outerlist,body2list,lx,ly,lz,maxrad):
     neighborlist=[]
     if any([len(body2list)==0,len(outerlist)==0]):
                     return body1list,body2list
     else:
        if len(outerlist)&gt;1:
           for mem1 in outerlist:
               for mem2 in body2list:
                    if overlap(mem1,mem2,maxrad,lx,ly,lz)==1:
                              neighborlist.append(mem2)
        if len(outerlist)==1:
           for mem2 in body2list:
               if overlap(outerlist[0],mem2,maxrad,lx,ly,lz)==1:
                              neighborlist.append(mem2)
        body1list=neighborlist+body1list
        if len(neighborlist)!=0:
           for mem3 in neighborlist:
               dummy=copy.deepcopy(mem3)
               if dummy in body2list:
                  print "True"
               else:
                  print "false"
               body2list.remove(dummy)
        return cluster(body1list,neighborlist,body2list,lx,ly,lz,maxrad)          
</code></pre>

<p>The error message is </p>

<pre><code>File "aggregation.py", line 107, in cluster
   body2list.remove(dummy) 
ValueError: list.remove(x): x not in list 
</code></pre>

<p>As you can see, the item to be removed was picked up from the list on which the remove method is being applied. Regarding the usage of dummy, I used this when directly removing mem3 gave me errors. This was part of my debugging process.</p>

<p>Update: Finally identified the source of error. Thanks to one of the members (Bharel) who tracked the loophole in the code. Items were being added multiple times to neighborlist, thus causing an error when I tried to remove them from body2list. The correct code would look like this:</p>

<pre><code>    if len(outerlist)&gt;1:
       for mem1 in outerlist:
       for mem2 in body2list:
          if overlap(mem1,mem2,maxrad,lx,ly,lz)==1:
                           if mem2 in neighborlist:
                                   pass
                           else:
                                   neighborlist.append(mem2)
</code></pre>

<p>This worked and gives results in agreement with imaging results when tested on small dilute systems. Thanks for your patience.</p>