7.4. Mapping Setitem

  • Adds if value not exist

  • Updates if value exist

7.4.1. 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'] = 'Alex Vogel'
>>> print(crew)  
{'commander': 'Alex Vogel',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez'}

7.4.2. 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='Alex Vogel')
>>> print(crew)  
{'commander': 'Alex Vogel',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez'}

7.4.3. Merge Operator

  • Merge (|) and update (|=) operators have been added to the built-in dict class.

  • Since Python 3.9: PEP 584 -- Add Union Operators To dict

>>> crew = {
...     'commander': 'Melissa Lewis',
...     'botanist': 'Mark Watney',
...     'pilot': 'Rick Martinez',
... }
>>>
>>> new = {
...     'chemist': 'Alex Vogel',
...     'surgeon': 'Chris Beck',
...     'engineer': 'Beth Johanssen',
... }
>>>
>>>
>>> everyone = crew | new
>>>
>>> print(crew)  
{'commander': 'Melissa Lewis',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez'}
>>>
>>> print(new)  
{'chemist': 'Alex Vogel',
 'surgeon': 'Chris Beck',
 'engineer': 'Beth Johanssen'}
>>>
>>> print(everyone)  
{'commander': 'Melissa Lewis',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez',
 'chemist': 'Alex Vogel',
 'surgeon': 'Chris Beck',
 'engineer': 'Beth Johanssen'}

7.4.4. Increment Merge Operator

>>> crew = {
...     'commander': 'Melissa Lewis',
...     'botanist': 'Mark Watney',
...     'pilot': 'Rick Martinez',
... }
>>>
>>> new = {
...     'chemist': 'Alex Vogel',
...     'surgeon': 'Chris Beck',
...     'engineer': 'Beth Johanssen',
... }
>>>
>>>
>>> crew |= new
>>>
>>> print(crew)  
{'commander': 'Melissa Lewis',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez',
 'chemist': 'Alex Vogel',
 'surgeon': 'Chris Beck',
 'engineer': 'Beth Johanssen'}
>>>
>>> print(new)  
{'chemist': 'Alex Vogel',
 'surgeon': 'Chris Beck',
 'engineer': 'Beth Johanssen'}

7.4.5. Assignments

"""
* Assignment: Mapping Dict Setitem
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Use `result: dict`
    2. Change value for key 'group' to 'admins'
    3. Use setitem syntax
    4. Run doctests - all must succeed

Polish:
    1. Użyj `result: dict`
    2. Zmień wartość dla klucza 'group' na 'admins'
    3. Użyj składni setitem
    4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * dict[key] = value

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint

    >>> assert type(result) is dict, \
    'Variable `result` has invalid type, should be dict'

    >>> assert all(type(x) is str for x in result.keys()), \
    'All dict keys should be str'

    >>> assert 'firstname' in result.keys()
    >>> assert 'lastname' in result.keys()
    >>> assert 'group' in result.keys()

    >>> assert 'Mark' in result.values()
    >>> assert 'Watney' in result.values()
    >>> assert 'users' not in result.values()
    >>> assert 'admins' in result.values()

    >>> pprint(result, width=40, sort_dicts=False)
    {'firstname': 'Mark',
     'lastname': 'Watney',
     'group': 'admins'}
"""

result = {
    'firstname': 'Mark',
    'lastname': 'Watney',
    'group': 'users',
}

# Change value for key 'group' to 'admins'
# Use setitem syntax
# type: dict[str,str]
...

"""
* Assignment: Mapping Dict Update
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Use `result: dict`
    2. Change value for key 'group' to 'admins'
    3. Use `update` method
    4. Run doctests - all must succeed

Polish:
    1. Użyj `result: dict`
    2. Zmień wartość dla klucza 'group' na 'admins'
    3. Użyj metody `update`
    4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * dict.update(key=value)

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint

    >>> assert type(result) is dict, \
    'Variable `result` has invalid type, should be dict'

    >>> assert all(type(x) is str for x in result.keys()), \
    'All dict keys should be str'

    >>> assert 'firstname' in result.keys()
    >>> assert 'lastname' in result.keys()
    >>> assert 'group' in result.keys()

    >>> assert 'Mark' in result.values()
    >>> assert 'Watney' in result.values()
    >>> assert 'users' not in result.values()
    >>> assert 'admins' in result.values()

    >>> pprint(result, width=40, sort_dicts=False)
    {'firstname': 'Mark',
     'lastname': 'Watney',
     'group': 'admins'}
"""

result = {
    'firstname': 'Mark',
    'lastname': 'Watney',
    'group': 'users',
}

# Change value for key 'group' to 'admins'
# Use `update` method
# type: dict[str,str]
...

"""
* Assignment: Mapping Dict UpdateMany
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Modify `result: dict`:
        a. change value for key 'firstname' to 'Melissa'
        b. change value for key 'lastname' to 'Lewis'
    2. Use `update` method
    3. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj `result: dict`:
        a. zmień wartość dla klucza 'firstname' na 'Melissa'
        b. zmień wartość dla klucza 'lastname' na 'Lewis'
    2. Użyj metodę `update`
    3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * dict.update()

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint

    >>> assert type(result) is dict, \
    'Variable `result` has invalid type, should be dict'

    >>> assert all(type(x) is str for x in result.keys()), \
    'All dict keys should be str'

    >>> assert 'firstname' in result.keys()
    >>> assert 'lastname' in result.keys()
    >>> assert 'group' in result.keys()

    >>> assert 'Melissa' in result.values()
    >>> assert 'Lewis' in result.values()
    >>> assert 'users' in result.values()

    >>> pprint(result, width=40, sort_dicts=False)
    {'firstname': 'Melissa',
     'lastname': 'Lewis',
     'group': 'users'}
"""

result = {
    'firstname': 'Mark',
    'lastname': 'Watney',
    'group': 'users',
}

# Change value for key 'firstname' to 'Melissa'
# Change value for key 'lastname' to 'Lewis'
# Use `dict.update()` method
# type: dict[str,str]
...

"""
* Assignment: Mapping Dict Union
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Use `DATA_A: dict`
    2. Use `DATA_B: dict`
    3. Define `result: dict` with union of `DATA_A` and `DATA_B`
    4. Run doctests - all must succeed

Polish:
    1. Użyj `DATA_A: dict`
    2. Użyj `DATA_B: dict`
    3. Zdefiniuj `result: dict` z unią `DATA_A` i `DATA_B`
    4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * |

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint
    >>> import string

    >>> assert result is not Ellipsis, \
    'Assign your result to variable `result`'
    >>> assert type(result) is dict, \
    'Variable `result` has invalid type, should be dict'

    >>> pprint(result, width=40, sort_dicts=True)
    {'botanist': 'Mark Watney',
     'chemist': 'Alex Vogel',
     'commander': 'Melissa Lewis',
     'engineer': 'Beth Johanssen',
     'pilot': 'Rick Martinez',
     'surgeon': 'Chris Beck'}
"""


DATA_A = {
    'commander': 'Melissa Lewis',
    'botanist': 'Mark Watney',
    'pilot': 'Rick Martinez',
}

DATA_B = {
    'chemist': 'Alex Vogel',
    'engineer': 'Beth Johanssen',
    'surgeon': 'Chris Beck',
}

# Define `result: dict` with union of `DATA_A` and `DATA_B`
# type: dict[str, str]
result = ...