adding values to keys

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Feb 15 09:45:10 CET 2008


Dennis Lee Bieber a écrit :
> On Thu, 14 Feb 2008 23:55:21 -0800 (PST), Brandon
> <your.master at gmail.com> declaimed the following in comp.lang.python:
(snip)
>> MT.fromkeys(NT[0], range(alpha,omega))
> 
> 	Note that NT is a single tuple -- your previous loop throws away the
> prior value and binds a new tuple each time. AND IT IS A TUPLE = ('z',
> [0, 1, 2, 3]), NT[0] is just "z" -- it does not have "keys" to use in
> the "fromkeys()" method.

Note that the first arg to dict.fromkeys doesn't need to have a .keys 
method - you can pass in any iterable, and even a single hashable (in 
which case your ditc will only have one key, of course).

 >>> dict.fromkeys('abc')
{'a': None, 'c': None, 'b': None}
 >>> dict.fromkeys(c for c in 'abc')
{'a': None, 'c': None, 'b': None}
 >>> dict.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
 >>> dict.fromkeys('a')
{'a': None}





More information about the Python-list mailing list