user3378508
user3378508

Reputation: 229

Update one dictionary with another in python

I am trying to send a dictionary key through command line. sys.argv[1] has the key. This key is store in var2 and send to append_func which is a another file append. dynamically whenever I give command line arguments for key, I want dictionary to get appended. But its overwriting everytime when ever i enter new commandline argument

Please look at the code :

##filename : dictionary.py## 

import sys
import append
dic={}
var1=sys.argv[0]
var2=sys.argv[1]
dicto=append.append_fun(var2,dic)
dic=dic.update(dicto)
print dic

##filename : append.py##
def append_fun(var2,dic1):
    dic1[var2] = "hello"
    return dic1

In the command line i am calling dictionary.py test. Again when i call, dictionary.py test1 it does not append dic with both values, it will overwrite with second one.

Please help me how to append dic dictionary

Upvotes: 2

Views: 1860

Answers (3)

wim
wim

Reputation: 362726

You seem to want persistence, so that you can call:

python dictionary.py test

And then later execute again:

python dictionary.py test1

whilst having the results from the first run remain. But nowhere are you saving the data from the first run, and it can not work like this currently because of the line dic={} which is executed each time initialising the empty dict.

To get this you will need to save state somewhere, say in a temporary file or a database. Instead of the line dic={}, your code must reload the previous state at startup (and, of course, you must also dump the current state before exit).

I recommend taking a look at pickle for a simple and easy serialization of python objects to disk.

Upvotes: 2

holdenweb
holdenweb

Reputation: 37033

Before we get around to your question, you should really take this code to codereview.stackexchange.com . I find it difficult to understand why you preferred

dicto=append.append_fun(var2,dic)

to

dicto[var2] = "hello"

If I am understanding you correctly, you would like a dictionary that, when you assign to it, appends the assigned value to any others that may have already been stored against that key? If so, are you going to love collections.defaultdict. I hope. Though it won't quite do that, I think it's capable of providing you with what they want.

When you create a defaultdict you call the class with one argument, a function that will be called to create a new value when an absent key is retrieved. Since list() returns an empty list attempting to retrieve any key will cause that key to be created and associated with a value created by calling the default object function (the one that was provided as an argument at object instantiation time). I hope the following interactive session will explain what you need to know, but if not see the documentation I linked to.

>>> import collections
>>> mydict = collections.defaultdict(list)
>>> mydict['newkey'].append("Surprise!") # empty list invisibly created
>>> mydict['newkey']
['Surprise!']
>>> mydict['newkey'].append("Surprise again!")
>>> mydict['newkey']
['Surprise!', 'Surprise again!']
>>> mydict['no such key']
[]
>>> list(mydict.keys())
['no such key', 'newkey']
>>>

Does this help, or have I been barking up the wrong tree?

Upvotes: 1

hivert
hivert

Reputation: 10667

In python dictionary are mutable object. Therefore, modification are done "in place". So you write

dic.update(dicto)

because update doesn't return anything. In the same vein:

def append_fun(var2,dic1):
    dic1[var2] = "hello"

and

append.append_fun(var2,dic)

Upvotes: 0

Related Questions