Pad or Truncate a List
Cut a list to a target length, or extend it with a fill value when it is shorter.
from typing import Any
def pad_or_truncate(items: list[Any], target_len: int, fill: Any = "N") -> list[Any]:
return items[:target_len] + [fill] * (target_len - len(items))
Caveats
The fill value is repeated by reference. Use an immutable fill such as a string. A negative target length slices from the end and does not pad.