Problem adding a Key Value pair

MRAB python at mrabarnett.plus.com
Tue Nov 4 21:19:32 CET 2014


On 2014-11-04 19:37, Anurag Patibandla wrote:
>
> I am trying to add a key value pair of ("Priority":"1") to queue1, ("Priority":"2") to queue2, and ("Priority":"3") to queue3.
> When I just add ("Priority":"1") to queue1, it works.
> But when I run the above code, ("Priority":"3") is being added to all the queues.
> This looks trivial and I don't understand why this is happening. Is there something wrong with what I am doing?
>
> json_split = {}
> value = {"Status": "Submitted", "m_Controller": "Python"}
> a = range(31)
> del a[0]

That's better as:

a = range(1, 31)

> for i in a:
>      json_split[i] = value

Here the key will be whatever 'i' refers to (1..30) and the value will
be the dict referred to by 'value'.

Try adding the line:

print json_split[1] is json_split[2]

It'll print out 'True'; it's saying that they are the same dict.

You want the values of json_split to be _separate_ dicts.

The fix is simple. Just make a copy of the dict for each one:

for i in a:
       json_split[i] = dict(value)

[snip]




More information about the Python-list mailing list