maybe that thing in python 3 that someone mentioned is the answer, but otherwise i always think Python should admit something like this:<div><br></div><div>a, b, c, *d = list</div><div><br></div><div>i.e. if list were [1,2,3,4,5], you'd get a=1, b=2, c=3, d=[4, 5]</div>
<div><br></div><div>not that that solves the None problem, though i don't have any feature suggestions that would address that.</div><div><br></div><div><div class="gmail_quote">On Fri, Nov 27, 2009 at 7:18 AM, boblatest <span dir="ltr"><<a href="mailto:boblatest@googlemail.com">boblatest@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hello all,<br>
<br>
(sorry for posting from Google. I currently don't have access to my<br>
normal nntp account.)<br>
<br>
Here's my question: Given a list of onknown length, I'd like to be<br>
able to do the following:<br>
<br>
(a, b, c, d, e, f) = list<br>
<br>
If the list has fewer items than the tuple, I'd like the remaining<br>
tuple elements to be set to "None". If the list is longer, I'd like<br>
the excess elements to be ignored.<br>
<br>
The code snippet below does what I want, I was just wondering if there<br>
was an interesting "Pythonic" way of expressing the same thing.<br>
<br>
Thanks,<br>
robert<br>
<br>
def iter_inf(li, n):<br>
for i in range(n):<br>
if i < len(li):<br>
r = li[i]<br>
else:<br>
r = None<br>
i += 1<br>
yield r<br>
<br>
<br>
li = ['a', 'b', 'c']<br>
(a, b, c, d, e) = iter_inf(li, 5)<br>
print a, b, c, d, e<br>
<div><div></div><div class="h5">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br></div>