ToshiBoy,<br><br>You might want to take a look at the filter() function, it can also be used for the kind of the thing you're doing.<br><br><a href="http://docs.python.org/tut/node7.html#SECTION007130000000000000000">http://docs.python.org/tut/node7.html#SECTION007130000000000000000</a><br>
<br>>>> B = range(1,27)<br>>>> def test(b):<br>...     if b*b in B:<br>...             return True<br>...     else:<br>...             return False<br>... <br>>>> A = filter(test, B)<br>>>> A<br>
[1, 2, 3, 4, 5]<br><br>Daniel<br><br><div class="gmail_quote">On Sat, Jun 28, 2008 at 1:00 AM, ToshiBoy <<a href="mailto:ToshiBoy@gmail.com">ToshiBoy@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">On Jun 28, 2:48 pm, Mel <<a href="mailto:mwil...@the-wire.com">mwil...@the-wire.com</a>> wrote:<br>
> ToshiBoy wrote:<br>
> > I have two lists A and B that are both defined as range(1,27) I want<br>
> > to find the entries that are valid for A = BxB<br>
> [ ... ]<br>
> > I get, as expected 1,4,9,16,25 printed out being the only members of B<br>
> > where the condition is true, but when I print B I get:<br>
><br>
> > [1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]<br>
><br>
> > 1 to 5 is correct, but why doesn't the remove method remove 7 and<br>
> > above? What am I doing wrong here?<br>
><br>
> Try this:<br>
><br>
> A = range(1,27)<br>
> B = range(1,27)<br>
> C = []<br>
><br>
> for b in B:<br>
>     print "Trying", b<br>
>     if b*b in A:<br>
>         print b<br>
>         C.append (b)<br>
>     else:<br>
>         print "Removing", b<br>
>         B.remove(b)<br>
> print 'B', B<br>
> print 'C', C<br>
><br>
> The essential problem is that your `B.remove`s are pulling the rug out from<br>
> under your `for b in B:`.  There are ways to mess with B while you iterate.<br>
> Running though B backwards will do: `for b in B[::-1]:`, or iterating over<br>
> a copy of B: `for b in B[:]:` or `for b in list(B):`.  Leaving B alone and<br>
> building up the desired items in C is probably simplest.<br>
><br>
>         Mel.<br>
<br>
</div></div>Thank you, of course! :-) Didn't even think of that... that I was<br>
modifying my iterators...<br>
<br>
Thank you<br>
<div><div></div><div class="Wj3C7c">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>