5.9. Mapping SetItem

5.9.1. Rationale

  • Adds if value not exist

  • Updates if value exist

5.9.2. Set Item Method

>>> crew = {
...    'commander': 'Melissa Lewis',
...    'botanist': 'Mark Watney',
...    'pilot': 'Rick Martinez'}
>>>
>>>
>>> crew['chemist'] = 'Alex Vogel'
>>> print(crew)  
{'commander': 'Melissa Lewis',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez',
 'chemist': 'Alex Vogel'}
>>> crew = {
...    'commander': 'Melissa Lewis',
...    'botanist': 'Mark Watney',
...    'pilot': 'Rick Martinez'}
>>>
>>>
>>> crew['commander'] = 'Jan Twardowski'
>>> print(crew)  
{'commander': 'Jan Twardowski',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez'}

5.9.3. Update Method

>>> crew = {
...    'commander': 'Melissa Lewis',
...    'botanist': 'Mark Watney',
...    'pilot': 'Rick Martinez'}
>>>
>>>
>>> crew.update(chemist='Alex Vogel')
>>> print(crew)  
{'commander': 'Melissa Lewis',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez',
 'chemist': 'Alex Vogel'}
>>> crew = {
...    'commander': 'Melissa Lewis',
...    'botanist': 'Mark Watney',
...    'pilot': 'Rick Martinez'}
>>>
>>>
>>> crew.update(commander='Jan Twardowski')
>>> print(crew)  
{'commander': 'Jan Twardowski',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez'}

5.9.4. Assignments