[Tutor] a question about list

Kalle Svensson kalle at lysator.liu.se
Fri Nov 12 00:58:51 CET 2004


[Terry Carroll]
> On Thu, 11 Nov 2004, Max Noel wrote:
> 
> > a = [element for index, element in enumerate(a) if index not in b]
[...]
> Would you mind unwrapping and explaining this a bit?  I'd like to 
> understand it better.

a = [element                            # pick element
     for index, element in enumerate(a) # from each index, element
                                        # tuple returned by
					# enumerate(a) 
     if index not in b]                 # if the index is not in b

The key thing here is that "for index, element in enumerate(a)"
contains an implicit tuple assignment (like "a, b = c, d").  Making
the tuple more explicit might be a good idea:

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

is, in my mind, perhaps a bit clearer.

Peace,
  Kalle
-- 
http://juckapan.org/~kalle/
http://laxmasteriet.se/04-05/
http://www.baljan.studorg.liu.se/


More information about the Tutor mailing list