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

[pygame] Re: more basic python lists question



ah! google is my friend.

for f in filenames:
    if not f.endswith('.png')
        filenames.remove(f)

Along with lots of warnings to copy from one list to another rather
than delete on an iterating list. But really how much harm could it
do? lol... I think.

On Thu, Nov 17, 2011 at 3:24 PM, Sean Wolfe <ether.joe@xxxxxxxxx> wrote:
> I find myself often iterating a list looking to remove the
> undesireable elements. I want to do this:
>
> for f in filenames:
>    if not f.endswith('.png')
>        f.delete()
>
> But I find myself having to do this:
>
> for i in range(len(filenames))::
>    if not filenames[i].endswith(".png"):
>        del filenames[i]
>
>
> I'm wondering if there is a way to do the first option? I suppose I"m
> trying to delete what I'm examining at the same time which maybe is
> why it's problematic.
>
> Thanks!
>
>
> --
> A musician must make music, an artist must paint, a poet must write,
> if he is to be ultimately at peace with himself.
> - Abraham Maslow
>



-- 
A musician must make music, an artist must paint, a poet must write,
if he is to be ultimately at peace with himself.
- Abraham Maslow