__init__ with multiple list values

Chris Angelico rosuav at gmail.com
Sun Oct 30 11:15:21 EDT 2011


On Mon, Oct 31, 2011 at 2:02 AM, Gnarlodious <gnarlodious at gmail.com> wrote:
> Orders=[Order(x,y,z) for x,y,z in [ratio, bias, locus]]
>

Assuming that you intend to take the first element of each list, then
the second, and so on, you'll want to use zip():

Orders=[Order(x,y,z) for x,y,z in zip(ratio, bias, locus)]

With your syntax, Python iterates over a three-element list. The first
iteration, it looks at 'ratio' and tries to unpack that into x,y,z;
this doesn't work, because ratio has five elements. The second
iteration would try to unpack 'bias', and the third would go for
'locus'.

The zip function returns tuples of (ratio[N], bias[N], locus[N]) for
successive Ns:

>>> list(zip(ratio,bias,locus))
[(1, True, 'A'), (2, False, 'B'), (3, True, 'C'), (4, False, 'D'), (5,
True, 'E')]

ChrisA



More information about the Python-list mailing list