list unpack trick?
aurora
aurora00 at gmail.com
Sat Jan 22 13:03:27 EST 2005
Thanks. I'm just trying to see if there is some concise syntax available
without getting into obscurity. As for my purpose Siegmund's suggestion
works quite well.
The few forms you have suggested works. But as they refer to list multiple
times, it need a separate assignment statement like
list = s.split('=',1)
I am think more in the line of string.ljust(). So if we have a
list.ljust(length, filler), we can do something like
name, value = s.split('=',1).ljust(2,'')
I can always break it down into multiple lines. The good thing about list
unpacking is its a really compact and obvious syntax.
On Sat, 22 Jan 2005 08:34:27 +0100, Fredrik Lundh <fredrik at pythonware.com>
wrote:
...
>> So more generally, is there an easy way to pad a list into length of n
>> with filler items appended
>> at the end?
>
> some variants (with varying semantics):
>
> list = (list + n*[item])[:n]
>
> or
>
> list += (n - len(list)) * [item]
>
> or (readable):
>
> if len(list) < n:
> list.extend((n - len(list)) * [item])
>
> etc.
>
> </F>
More information about the Python-list
mailing list