Reputation: 8131
How do I create a dictionary in python that is passed to a function so it would look like this:
def foobar(dict):
dict = tempdict # I want tempdict to not point to dict, but to be a different dict
#logic that modifies tempdict
return tempdict
How do I do this?
Upvotes: 1
Views: 91
Reputation: 23624
You need to copy dict to tempdict.
def foobar(d):
temp = d.copy()
# your logic goes here
return temp
copy
makes a shallow copy of the dict (i.e. copying its values, but not its values' values).
% python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'x': 17, 'y': 23}
>>> t = d.copy()
>>> t
{'y': 23, 'x': 17}
>>> t['x'] = 93
>>> t
{'y': 23, 'x': 93}
>>> d
{'y': 23, 'x': 17}
>>>
Upvotes: 4