list unpack trick?

Alex Martelli aleaxit at
Sat Jan 22 06:10:36 EST 2005

Fredrik Lundh <fredrik at> wrote:

> Alex Martelli wrote:
> >> or (readable):
> >>
> >>     if len(list) < n:
> >>         list.extend((n - len(list)) * [item])
> >
> > I find it just as readable without the redundant if guard -- just:
> >
> >    alist.extend((n - len(alist)) * [item])
> the guard makes it obvious what's going on, also for a reader that doesn't
> know exactly how "*" behaves for negative counts.

It does, but it's still redundant, like, say,
    if x < 0:
        x = abs(x)
makes things obvious even for a reader not knowing exactly how abs
behaves for positive arguments.  Redundancy in the code to try and
compensate for a reader's lack of Python knowledge is not, IMHO, a
generally very productive strategy.  I understand perfectly well that
you and others may disagree, but I just thought it worth stating my
personal opinion in the matter.

>  once you've seen the
> "compare length to limit" and "extend", you don't have to parse the rest of
> the statement.

Sorry, I don't get this -- it seems to me that I _do_ still have to
"parse the rest of the statement" to understand exactly what alist is
being extended by.


More information about the Python-list mailing list