-2

is there a way to insert a dictionary into another dictionary without just creating a reference to the dictionary.e.g

dict_a = {}
dict_b = {}
dict_a.insert(key, value)

this would prevent the problems that come with

dict_a["somekey"] = dict_b
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
james
  • 919
  • 4
  • 11
  • 17
  • 1
    What problems are you trying to address? – mipadi Jan 03 '11 at 20:55
  • 3
    See your last question, `copy` was already mentioned. You went over the same basic problem in 3 questions now, maybe it's time to learn how Python works and how work *with* it? – Jochen Ritzel Jan 03 '11 at 20:58
  • actually thc my first question was about variable assignment, my second about copying, and this one, about a different way to insert key and value pairs into a dictionary. id think its fairly different :) either way thanks for your unhelpfull input :) – james Jan 03 '11 at 21:04
  • 4
    @james: Yeah, it's three different questions. But they all go back to the same issue: Python works by reference, not by value. So although the questions are different, the answers are the same. – Lennart Regebro Jan 03 '11 at 21:09
  • 1
    You don't. It's always references all the way down (until you get to the turtles ^^). That's how Python works, and we're all getting along just fine with it. Learn how to use it, don't try to avoid it. –  Jan 03 '11 at 21:33
  • 2
    @james: "id think its fairly different". Take the hint. They're not actually different. And **never** complain when people are trying to help you. It's rude to ask for help, get help and reject that help. You've been advised how Python works. Please try to understand the advise instead of rejecting it. – S.Lott Jan 03 '11 at 22:29

5 Answers5

4

You could use copy.deepcopy on b:

>>> a = {'a':[1,2,3]}
>>> b = {'b':[4,5,6]}
>>> a['c'] = copy.deepcopy(b)
>>> a
{'a': [1, 2, 3], 'c': {'b': [4, 5, 6]}}
>>> b
{'b': [4, 5, 6]}
>>> b['b'].append(7)
>>> b
{'b': [4, 5, 6, 7]}
>>> a
{'a': [1, 2, 3], 'c': {'b': [4, 5, 6]}}

Using update or copy as above will perform a shallow copy.

sizzzzlerz
  • 4,277
  • 3
  • 27
  • 35
2

Yes.

 dict_a.update(dict_b)

This will insert all the keys/values from dict_b into dict_a (note: this is in-place and returns None)

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
1

You need to make a copy:

dict_a["dsomekey"] = dict_b.copy()

Edit: There is no dictionary method that inserts the value by-value. Python dictionaries always use references.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
1

I think you may be meaning update?

>>> dict_a = {1: 'a'}
>>> dict_b = {2: 'b'}
>>> dict_a.update(dict_b)
>>> dict_a
{1: 'a', 2: 'b'}

Or you mean that you want a copy?

>>> from copy import copy
>>> dict_a = {1: 'a'}
>>> dict_b = {2: 'b'}
>>> dict_a['dict'] = copy(dict_b)
>>> dict_a
{1: 'a', 'dict': {2: 'b'}}
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
1

EDIT: this answer is better. You should use copy.deepcopy or else you'll get references to objects stored in the dictionary-to-be-copied in the copied dictionary.

Original answer below:

You need to explicitly create a copy of the second dictionary, and set that as the value in the original dictionary.

dict_a["somekey"] = dict_b.copy()

When you copy a dictionary, it does exactly what's on the tin, it creates a brand new copy of the original dictionary.

Community
  • 1
  • 1
Alex Vidal
  • 4,080
  • 20
  • 23