Most efficient solution?

Peter Hansen peter at engcorp.com
Mon Jul 16 21:27:18 EDT 2001


William Park wrote:
> On Mon, Jul 16, 2001 at 06:03:27PM +0200, Alex Martelli wrote:
> > "Jay Parlar" <jparlar at home.com> wrote:
> >     ...
> > > for eachItem in A:
> > >     if eachItem in B:
> > >         A.remove(eachItem)
> > >
> > > Now, this will work fine,
> >
> > Are you sure about that?  Normally, modifying the list
> > you're iterating on does NOT work fine.
> 
> Good point.  Most of us knew what he meant, though.

(Yeah, but only Alex knew that what he meant was wrong...)

I find humour (and humor) in these discussions, on several
fronts.

1. Python appears to be a language with an incredibly wide
   range of potential optimizations.  The generic "vanilla"
   idiom (is that triply redundant?) posted by many people
   often appears to be very similar, in comparison to 
   other languages (the likely source of the "one way to 
   write it" mantra).  As refactoring goes on, the performance
   probably improves several orders of magnitude.

2. These are wonderful examples of just how many excellent
   programmers can post incorrect code.  Sure, relatively
   little time was spent developing it or, obviously, 
   testing it before posting.  Still it should serve to
   spread a little humility on us all.

3. We certainly talk a lot about "one way to write it", 
   but obviously Python does allow several ways in most
   cases.  It seems to split roughly between the "most
   obvious" (probably only for us newcomers) and the 
   map/filter/lambda/list comprehension style which is
   obviously second nature for some and entirely outside
   of the experience of others.  This ties back to
   (1), suggesting that learning more about these builtins
   is one of the best ways to optimize (working) Python
   code.

4. Pythonistas spend too much time writing working code,
   so they obviously need an "optimization fix" from
   this newsgroup from time to time!  :-)

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list