understanding list scope

Tino Wildenhain tino at wildenhain.de
Mon Sep 22 09:50:46 EDT 2008


Hi,


Alex wrote:
> Hi all!
> 
> I have a problem understanding the behaviour of this snippet:
> 
> data_set = ({"param":"a"},{"param":"b"},{"param":"c"})
> 
> for i in range(len(data_set)):
>     ds = data_set[:]
>     data = ds[i]
>     if i == 1: data['param'] = "y"
>     if i == 2: data['param'] = "x"

> 
> print data_set

Beside the data copy which is explained in the other posts,
what is the problem you want to solve? Would

data_set[1]['param']="x"
data_set[2]['param']="y"

not doing the same?

or more to your point:

ds=data_set[:1]+[dict(param="x"),dict(param="y")]+data_set[2:]

HTH
Tino
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20080922/aee11c7b/attachment-0001.bin>


More information about the Python-list mailing list