Removing None objects from a sequence

pruebauno at latinmail.com pruebauno at latinmail.com
Fri Dec 12 10:03:44 EST 2008


On Dec 12, 8:08 am, "Filip Gruszczyński" <grusz... at gmail.com> wrote:
> I am not doing it, because I need it. I can as well use "if not elem
> is None", but I just wanted to know, if there is some better way of
> doing this. I like to know :-)
>
> And I can't understand why you are becoming so aggressive because of
> this. Just because I asked for that, doesn't mean, that I will put
> some obfuscated code into my project. I just wanted to learn something
> new - I checked itertools, I googled a bit, now I wanted to ask here
> if someone knew some really cool way of this.  All the other
> assumptions weren't really necessary.
>
> Thanks for those ideas, however. I like the last one a lot :)
>
> --
> Filip Gruszczyński

In this case the "cool" way is the straightforward way:
either the list versions:

[x fox x in seq if x is not None]
filter(lambda x: x is not None, seq)

or the generator versions:

(x for x in seq if x is not None)
itertools.ifilter(lambda x: x is not None, seq)




More information about the Python-list mailing list