[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Concerning copy



In my limited experience with Python/PyGame, it seems to me like list
comprehensions are a good way around this "bug".

Instead of

def main():
    nums = [x for x in range(10)]
    for num in nums:
         if num % 2 = 0:
               nums.remove(num)

I use

def main():
    nums = [x for x in range(10)]
    nums = [num for num in nums if num % 2 == 0]

(Of course, there's better ways to construct the list in the first
place, but you get the idea.)

-Nate

On 26/04/06, Kamilche <klachemin@xxxxxxxxxxx> wrote:
> The example could have been shorter.
> Here it is with using numbers, lists, and no classes.
>
> def main():
>      nums = [x for x in range(10)]
>      print nums
>      for num in nums:
>          nums.remove(num)
>      print nums
>
>
>