how to store the variable in dictionary code example

Example 1: how to store the variable in dictionary

>>> mydict = {'a': 'hello', 'b': 'world'}
>>> for x in mydict:
...    val = mydict[x]
...    mydict[x] = val.upper()
...    print('Changed what', x, 'points to: from', val, 'to', mydict[x])
Changed what b points to: from world to WORLD
Changed what a points to: from hello to HELLO

Example 2: how to store the variable in dictionary

>>> mydict = {"message": {"hello": 123456}}
>>> print(mydict['message'])
{'hello': 123456}
>>> print(mydict['message']['hello'])
123456

Tags:

Misc Example