Making a dict from two lists/tuples
Duncan Booth
duncan at NOSPAMrcp.co.uk
Thu May 24 11:24:58 EDT 2001
Jonas Meyer Rasmussen <meyer at diku.dk> wrote in
news:Pine.LNX.4.21.0105241705001.27131-100000 at berling.diku.dk:
> On Thu, 24 May 2001, Duncan Booth wrote:
>
>> "Jonas Meyer Rasmussen" <meyer at kampsax.dtu.dk> wrote in
>> news:9eiljf$jg1$1 at eising.k-net.dk:
>>
>> > for key,value in keys,values
>> > dict[key] = value
>>
>> Bzzzt! Wrong answer.
>
> Why is it wrong??
> it worked fine on my machine(2.1, win32).
Ignoring for a moment the missing colon, the main problem is that I think
you tested it with lists of two elements. For any other length it
immediately becomes obvious that something is wrong:
>>> keys = ['a', 'b', 'c']
>>> values = [1, 2, 3]
>>> dict = {}
>>> for key,value in keys, values:
... dict[key] = value
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: unpack list of wrong size
>
> And what is zip supposed to do, i'm trying it here on another machine,
> which gives me a NameError(1.6 Linux)
zip(list1, list2) in Python 2.1 is roughly equivalent to map(None, list1,
list2) except that if the lists are a different length the behaviour is
somewhat different. It collects corresponding elements from each list
together into a tuple.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list