list unpack trick?

Fredrik Lundh fredrik at
Sat Jan 22 05:25:08 EST 2005

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.  once you've seen the
"compare length to limit" and "extend", you don't have to parse the rest of
the statement.


More information about the Python-list mailing list