Filling in a tuple from unknown size list

inhahe inhahe at gmail.com
Sun Nov 29 10:42:30 CET 2009


maybe that thing in python 3 that someone mentioned is the answer, but
otherwise i always think Python should admit something like this:

a, b, c, *d = list

i.e. if list were [1,2,3,4,5], you'd get a=1, b=2, c=3, d=[4, 5]

not that that solves the None problem, though i don't have any feature
suggestions that would address that.

On Fri, Nov 27, 2009 at 7:18 AM, boblatest <boblatest at googlemail.com> wrote:

> Hello all,
>
> (sorry for posting from Google. I currently don't have access to my
> normal nntp account.)
>
> Here's my question: Given a list of onknown length, I'd like to be
> able to do the following:
>
> (a, b, c, d, e, f) = list
>
> If the list has fewer items than the tuple, I'd like the remaining
> tuple elements to be set to "None". If the list is longer, I'd like
> the excess elements to be ignored.
>
> The code snippet below does what I want, I was just wondering if there
> was an interesting "Pythonic" way of expressing the same thing.
>
> Thanks,
> robert
>
> def iter_inf(li, n):
>    for i in range(n):
>        if i < len(li):
>            r = li[i]
>        else:
>            r = None
>        i += 1
>        yield r
>
>
> li = ['a', 'b', 'c']
> (a, b, c, d, e) =  iter_inf(li, 5)
> print a, b, c, d, e
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091129/1e1d3a96/attachment.html>


More information about the Python-list mailing list