Hi,
Is it possible to add a key to a python dictionary after it has been created? It doesn't seem to have an .add() method...
Hi,
Is it possible to add a key to a python dictionary after it has been created? It doesn't seem to have an .add() method...
>>> d = {'key':'value'}
>>> print d
{'key': 'value'}
>>> d['mynewkey'] = 'mynewvalue'
>>> print d
{'mynewkey': 'mynewvalue', 'key': 'value'}
Yeah, it's pretty easy. Just do the following:
dict["key"] = "value"
>>> x = {1:2}
>>> print x
{1: 2}
>>> x.update({3:4})
>>> print x
{1: 2, 3: 4}