How to rewrite this as list comprehension?

Remco Gerlich scarblac at
Tue Dec 19 09:34:26 EST 2000

Serge Beaumont <serge at> wrote in comp.lang.python:
> I'm trying to get my head around comprehensions. How would I rewrite this
> method?
>   def deadThings(self, type = None):
>     deadThings = self.destroyed[:]
>     for thing in self.things:
>       if thing.destroyed:
>         if type:
>           if isinstance(thing, type):
>             deadThings.append(thing)
>         else:
>           deadThings.append(thing)
>     return deadThings

def deadThings(self, type = None):
   return (self.destroyed + 
           [thing for thing in self.things
	          if thing.destroyed
                  if not type or isinstance(thing, type)])

I'm still in doubt whether they're really better.

Remco Gerlich

More information about the Python-list mailing list