Re: default value in a list

2005-01-24 Thread TB
Thanks very much for all the responses. They were useful to me and I'll probably refer back to them again in the future. TB -- http://mail.python.org/mailman/listinfo/python-list

Re: default value in a list

2005-01-22 Thread Alex Martelli
TB [EMAIL PROTECTED] wrote: Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? import itertools as it a, b, c = it.islice( it.chain(

Re: default value in a list

2005-01-22 Thread Peter Otten
Paul McGuire wrote: Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? I asked a very similar question a few weeks ago, and from the various

Re: default value in a list

2005-01-22 Thread Peter Otten
Peter Otten wrote: Paul McGuire wrote: Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? I asked a very similar question a few weeks ago, and

Re: default value in a list

2005-01-22 Thread Nick Craig-Wood
TB [EMAIL PROTECTED] wrote: Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? You could use this old trick... a, b, c =

Re: default value in a list

2005-01-22 Thread Alex Martelli
Nick Craig-Wood [EMAIL PROTECTED] wrote: ... Or this version if you want something other than as the default a, b, b = (line.split(':') + 3*[None])[:3] Either you mean a, b, c -- or you're being subtler than I'm grasping. BTW This is a feature I miss from perl... Hmmm, I understand

Re: default value in a list

2005-01-22 Thread Bengt Richter
On Fri, 21 Jan 2005 17:04:11 -0800, Jeff Shannon [EMAIL PROTECTED] wrote: TB wrote: Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields?

Re: default value in a list

2005-01-22 Thread Nick Craig-Wood
Alex Martelli [EMAIL PROTECTED] wrote: Nick Craig-Wood [EMAIL PROTECTED] wrote: ... Or this version if you want something other than as the default a, b, b = (line.split(':') + 3*[None])[:3] Either you mean a, b, c -- or you're being subtler than I'm grasping. Just a typo -

Re: default value in a list

2005-01-22 Thread Michael Spencer
Alex Martelli wrote: [explanation and the following code:] a, b, c = it.islice( ... it.chain( ... line.split(':'), ... it.repeat(some_default), ... ), ... 3) ... ... def pad_with_default(N, iterable,

Re: default value in a list

2005-01-22 Thread Reinhold Birkenfeld
Michael Spencer wrote: Alex Martelli wrote: [explanation and the following code:] a, b, c = it.islice( ... it.chain( ... line.split(':'), ... it.repeat(some_default), ... ), ... 3) ... ...

Re: default value in a list

2005-01-22 Thread Nick Coghlan
Reinhold Birkenfeld wrote: Why not put these together and put it in itertools, since the requirement seems to crop up every other week? line = A:B:C.split(:) ... def ipad(N,iterable, default = None): ... return it.islice(it.chain(iterable, it.repeat(default)), N) ... a,b,c,d =

Re: default value in a list

2005-01-22 Thread Reinhold Birkenfeld
Nick Coghlan wrote: Reinhold Birkenfeld wrote: Why not put these together and put it in itertools, since the requirement seems to crop up every other week? line = A:B:C.split(:) ... def ipad(N,iterable, default = None): ... return it.islice(it.chain(iterable, it.repeat(default)),

Re: default value in a list

2005-01-21 Thread Steve Holden
TB wrote: Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? l = line.split(':') l is a list, whose length will be one more than the number of colons in

Re: default value in a list

2005-01-21 Thread Paul McGuire
TB [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? Thanks, TB I asked a very

Re: default value in a list

2005-01-21 Thread Steven Bethard
Paul McGuire wrote: expand = lambda lst,default,minlen : (lst + [default]*minlen)[0:minlen] Or if you're afraid of lambda like me: def expand(lst,default,minlen):return (lst + [default]*minlen)[0:minlen] or perhaps more readably: def expand(lst, default, minlen): return (lst +

Re: default value in a list

2005-01-21 Thread Jeff Shannon
TB wrote: Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: a, b, c = (line.split(':')) if line could have less than three fields? (Note that you're actually assigning to a group of local variables, via tuple unpacking,

Re: default value in a list

2005-01-21 Thread Fredrik Lundh
Paul McGuire wrote: I asked a very similar question a few weeks ago, and from the various suggestions, I came up with this: expand = lambda lst,default,minlen : (lst + [default]*minlen)[0:minlen] I wouldn't trust whoever suggested that. if you want a function, use a function: def