[Tutor] a question about list

Max Noel maxnoel_fr at yahoo.fr
Fri Nov 12 00:33:02 CET 2004

On Nov 11, 2004, at 23:17, Terry Carroll wrote:

> A kludge would be to delete in right-to-left order instead:
>  b_rev = b
>  b_rev.sort()      # b = [1, 2, 4] ; sorted just to be sure
>  b_rev.reverse()   # b = [4, 2, 1]
>  for i in b_rev:
>    del a[i]
> But that's pretty ugly.  I'll bet someone has responded with a far more
> pythonic way of doing this as I've been typing.

a = [element for index, element in enumerate(a) if index not in b]

	Looks a bit more Pythonic to me. I also think it's faster. However, it 
uses more memory: in the "worst" case, you'll find yourself with a copy 
of a in memory for a short moment. Probably not a big deal, though, 
unless a is already occupying more than half your computer's memory.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"

More information about the Tutor mailing list