7.5. Mapping Delitem
7.5.1. Pop Method
dict.pop(key)
- removes and returns the item with the specified key from the dictionary
>>> data = {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
>>>
>>> result = data.pop('age')
>>>
>>> print(result)
41
>>>
>>> print(data)
{'firstname': 'Mark', 'lastname': 'Watney'}
7.5.2. Popitem Method
dict.popitem()
- removes and returns last item (key-value pair) from the dictionary
>>> data = {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
>>>
>>> result = data.popitem()
>>>
>>> print(result)
('age', 41)
>>>
>>> print(data)
{'firstname': 'Mark', 'lastname': 'Watney'}
7.5.3. Clear
dict.clear()
- removes all items from the dictionary
>>> data = {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
>>>
>>> result = data.clear()
>>>
>>> print(result)
None
>>>
>>> print(data)
{}
7.5.4. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Mapping Dict Pop
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Use `DATA: dict`
# 2. Define `result: str` with value popped for key 'group'
# 3. Use `pop` method
# 4. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA: dict`
# 2. Zdefiniuj `result: str` z wartością dla klucza 'group'
# 3. Użyj metodę `pop`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `dict.pop(key)`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> assert 'firstname' in DATA.keys()
>>> assert 'lastname' in DATA.keys()
>>> assert 'group' not in DATA.keys()
>>> assert 'Mark' in DATA.values()
>>> assert 'Watney' in DATA.values()
>>> assert 'users' not in DATA.values()
>>> pprint(result)
'users'
"""
DATA = {
'firstname': 'Mark',
'lastname': 'Watney',
'group': 'users',
}
# Define `result: str` with value popped for key 'group'
# Use `pop` method
# type: str
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Mapping Dict Pop
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define `result: tuple` with last key-value pair from `DATA`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: tuple` z ostatnią parą klucz-wartość z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `dict.pop(key)`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> assert type(result) is tuple, \
'Variable `result` has invalid type, should be tuple'
>>> assert 'firstname' in DATA.keys()
>>> assert 'lastname' in DATA.keys()
>>> assert 'group' not in DATA.keys()
>>> assert 'Mark' in DATA.values()
>>> assert 'Watney' in DATA.values()
>>> assert 'users' not in DATA.values()
>>> pprint(result)
('group', 'users')
"""
DATA = {
'firstname': 'Mark',
'lastname': 'Watney',
'group': 'users',
}
# Define `result: tuple` with last key-value pair from `DATA`
# type: str
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Mapping Dict Clear
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Use `DATA: dict`
# 2. Define `result: None` with result of clearing all values
# 3. Use `clear` method
# 4. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA: dict`
# 2. Zdefiniuj `result: None` z wynikiem wyczyszczenia wszystkich wartości
# 3. Użyj metodę `clear`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `dict.clear()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> assert result is None, \
'Variable `result` has invalid type, should be dict'
>>> assert type(DATA) is dict, \
'Variable `DATA` has invalid type, should be dict'
>>> assert DATA == {}, \
'Variable `DATA` has not been cleared properly'
>>> pprint(result)
None
"""
DATA = {
'firstname': 'Mark',
'lastname': 'Watney',
'group': 'users',
}
# Define `result: None` with result of clearing all values
# Use `clear` method
# type: None
result = ...