adding values to keys

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Feb 15 03:34:31 EST 2008


Brandon a écrit :
> Hi all,
> 
> I'm not sure if I'm calling the right method in a dictionary.  I have:
> 
> for k,v in dict.items():

don't use 'dict' as an identifier, this shadows the builtin dict type.

>      NT = k,range(alpha,omega)        #where alpha and omega are
> previously defined as 1 and 4, respectively
>      print NT

If you don't care about the values, you should iterate directly over the 
keys - which is the default for dicts, ie:

for key in somedict:
    print k

Also, by convention, ALL_CAPS names denote (pseudo) symbolic constants.

> which gives:
> ('w', [0,1,2,3])
> ('x', [0,1,2,3])
> ('y', [0,1,2,3])
> ('z', [0,1,2,3])

and by that time, NT == ('z', [0,1,2,3])

> And now I want a master dictionary like: [{'w': [0],[1],[2],[3]},
> {'x': [0]...]

This is a list of dicts, each one having a single key pointing to a 
tuple of four one-element lists. Are you *sure* this is *really* what 
you want ?

> So I try:
> 
> MT = {}

this creates an empty dict instance...

> MT.fromkeys(NT[0], range(alpha,omega))

this calls the classmethod dict.fromkeys() on the empty dict instance 
created by the previous statement, and discards the dict instance 
created by the call to fromkeys().

Also, since at this stage NT is ('z', [0,1,2,3]), NT[0] is 'z', so the 
dict created by fromkeys (and happily discarded) looked like:

{'z': [0, 1, 2, 3]}


> print MT
> 
> but this only returns:
> {}

Indeed. You defined MT as an empty dict, didn't you ?

> {}
> {}...
> 
> Anybody see what I'm doing wrong? 

Quite a lot of things actually, but the worst one is probably failing to 
  read the FineManual(tm) !-)

Assuming that you have a dict d, and want to build another dict with d 
keys and range(alpha,omega) for values, here's the solution:


alpha = 0
omega = 4

# arbitrary values, just for the exemple
d = dict(w=1, x=2, y=3, z=4)

master = dict.fromkeys(d, range(alpha, omega))

print master
=> {'y': [0, 1, 2, 3], 'x': [0, 1, 2, 3], 'z': [0, 1, 2, 3], 'w': [0, 1, 
2, 3]}

Now note that this will associate each key of master with the *same* 
list instance, so:

master['y'].append(42)
 >>> print master
{'y': [0, 1, 2, 3, 42], 'x': [0, 1, 2, 3, 42], 'z': [0, 1, 2, 3, 42], 
'w': [0, 1, 2, 3, 42]}

which is perhaps not what you want !-)

If you want distinct lists, dict.fromkeys is not the right method. You'd 
better use the default constructor, passing it a sequence of key,value 
tuples, ie:

master = dict((k, range(0,4)) for k in d)
print master
=> {'y': [0, 1, 2, 3], 'x': [0, 1, 2, 3], 'z': [0, 1, 2, 3], 'w': [0, 1, 
2, 3]}
master['y'].append(42)
print master
{'y': [0, 1, 2, 3, 42], 'x': [0, 1, 2, 3], 'z': [0, 1, 2, 3], 'w': [0, 
1, 2, 3]}


> Any advice is much appreciated.

Ok:
- read the FineManual(tm)
- learn to use the interactive Python shell
- read the FineManual(tm)
- learn to use the help feature of the interactive Python shell
- read the FineManual(tm)
- read pep08 on naming conventions
- read the FineManual(tm)

!-)

HTH



More information about the Python-list mailing list