10.13. Iterator Starmap
Lazy evaluated
itertools.starmap(function, iterable)
>>> from itertools import starmap
>>>
>>>
>>> data = starmap(pow, [(2,5), (3,2), (10,3)])
>>>
>>> next(data)
32
>>> next(data)
9
>>> next(data)
1000
>>> next(data)
Traceback (most recent call last):
StopIteration
10.13.1. Assignments
# %% About
# - Name: Iterator Starmap Args
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% English
# 1. Use `starmap()` to create `list[User]` from `DATA`
# 2. Define variable `result` with `list[User]` objects
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `starmap()` do stworzenia `list[User]` z `DATA`
# 2. Zdefiniuj zmienną `result` z obiektami `list[User]`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# [User(firstname='Alice', lastname='Apricot', email='alice@example.com'),
# User(firstname='Bob', lastname='Banana', email='bob@example.com'),
# User(firstname='Carol', lastname='Corn', email='carol@example.com'),
# User(firstname='Dave', lastname='Durian', email='dave@example.org'),
# User(firstname='Eve', lastname='Elderberry', email='eve@example.org'),
# User(firstname='Mallory', lastname='Melon', email='mallory@example.net')]
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is starmap, \
'Variable `result` has invalid type, should be starmap'
>>> result = list(result)
>>> assert all(type(row) is User for row in result), \
'All elements in result must be a User'
>>> from pprint import pprint
>>> pprint(result) # doctest: +NORMALIZE_WHITESPACE
[User(firstname='Alice', lastname='Apricot', email='alice@example.com'),
User(firstname='Bob', lastname='Banana', email='bob@example.com'),
User(firstname='Carol', lastname='Corn', email='carol@example.com'),
User(firstname='Dave', lastname='Durian', email='dave@example.org'),
User(firstname='Eve', lastname='Elderberry', email='eve@example.org'),
User(firstname='Mallory', lastname='Melon', email='mallory@example.net')]
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
from itertools import starmap
# %% Types
result: list['User']
# %% Data
class User:
def __init__(self, firstname, lastname, email):
self.firstname = firstname
self.lastname = lastname
self.email = email
def __repr__(self):
clsname = self.__class__.__name__
firstname = self.firstname
lastname = self.lastname
email = self.email
return f'{clsname}({firstname=}, {lastname=}, {email=})'
DATA = [
('firstname', 'lastname', 'email'),
('Alice', 'Apricot', 'alice@example.com'),
('Bob', 'Banana', 'bob@example.com'),
('Carol', 'Corn', 'carol@example.com'),
('Dave', 'Durian', 'dave@example.org'),
('Eve', 'Elderberry', 'eve@example.org'),
('Mallory', 'Melon', 'mallory@example.net'),
]
# %% Result