Problem with List of List

Fredrik Lundh fredrik at pythonware.com
Sat Aug 26 03:19:47 EDT 2006


Kirt wrote:

>> forgot to mention that the fix is to change the first for statement to:
>>
>>      for x in List[:]:
>>
>> </F>

> Thanx Fredrik Lundh for ur response. I tried ur fix But the output i am
> getting is repeated.

I'm pretty sure I told you to *loop* over a copy, not *remove* things 
from a copy.

> for x in List[:]:
>     t2=[]
>     print x[0]
>     for y in List:
>             if x[0]==y[0]:
>                     print y[1],y[2]
>                     t2.append(y)
> 
>     for z in t2:
>             List[:].remove(z)

what's that last line supposed to to?

here's your *original* code, changed to loop over a copy:

for x in List[:]:
      temp=[]
      print x
      for y in List:
              if x[0]==y[0]:
                      print y[0],y[1]
                      temp.append(y)
      for z in temp:
             List.remove(z)
             print 'rem', z

when run on your original data set, this prints your expected output.

</F>




More information about the Python-list mailing list