McA wrote:
Hi all,

probably a dumb question, but I didn't find something elegant for my
problem so far.
In perl you can unpack the element of a list to variables similar as
in python
(a, b, c = [0, 1, 2]), but the number of variables need not to fit the
number
of list elements.
That means, if you have less list elements variables are filled with
'undef' (None in python), if you have more list elements as necessary
the rest is ignored.

How can I achieve this behaviour with python in an elegant and fast
way?

Best regards
Andreas Mock
--
http://mail.python.org/mailman/listinfo/python-list

Python 3.0 has something a bit like this. Excess values can be bound (as a list) to the last variable:

 a,b,*c = [1,2,3,4,5]

will result in c containing [3,4,5].

In Python 2.x, you can't do that directly, but you should be able to create a function that lengthens or shortens an input tuple of arguments to the correct length so you can do:

 a,c,b = fix(1,2)
 d,e,f = fix(1,2,3,4)

However, the function won't know the length of the left hand side sequence, so it will have to be passed in as an extra parameter or hard coded.


Gary Herron

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to