Removing None objects from a sequence

Steve Holden steve at holdenweb.com
Fri Dec 12 08:27:59 EST 2008


Filip Gruszczyński wrote:
> Hi!
> 
> I would like to iterate over a sequence nad ignore all None objects.
> The most obvious way is explicitly checking if element is not None,
> but it takes too much space. And I would like to get something faster.
> I can use
> [ sth for sth in self.__sth if not sth is None ], but I don't know if
> that's the best way. I checked itertools, but the only thing that
> seemed ok, was ifilter - this requires seperate function though, so
> doesn't seem too short. How can I get it the shortest and fastest way?
> 
The problem with the list comprehension you quote above is that it
creates a new list, which costs both time and memory. You might want to
try using a generator expression instead:

(sth for sth in self.__sth if not sth is None)

This will give you the same sequence of values, but will produce them
only as they need to be consumed, saving the memory and compute overhead
of creating a second list.

>>> lst = [1, None, 3, None, "five", None]
>>> mylst = [l for l in lst if l is not None]
>>> mygen = (l for l in lst if l is not None)
>>> mylst, mygen
([1, 3, 'five'], <generator object at 0x92f290>)
>>> for l in mylst: print l,
...
1 3 five
>>> for l in mygen: print l,
...
1 3 five
>>>

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list