Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80231

Re: Problem adding a Key Value pair

Date 2014-11-04 20:19 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Problem adding a Key Value pair
References <32931127-fc4d-4ea5-87eb-f763a4fdf52c@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15472.1415132384.18130.python-list@python.org> (permalink)

Show all headers | View raw


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]

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Problem adding a Key Value pair Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-11-04 11:37 -0800
  Re: Problem adding a Key Value pair MRAB <python@mrabarnett.plus.com> - 2014-11-04 20:19 +0000
  Re: Problem adding a Key Value pair Anurag Patibandla <anuragpatibandla7@gmail.com> - 2014-11-04 12:56 -0800

csiph-web