UniCoreFW

splice()

Modify the contents of `array` by inserting elements starting at index `start` and removing `delete_count` number of elements after.

Implementation

Args: array: List to splice. start: Start to splice at. delete_count: Number of items to remove starting at `start`. If ``None`` then all items after `start` are removed. Defaults to ``None``. items: Elements to insert starting at `start`. Each item is inserted in the order given. Returns: The removed elements of `array` or the spliced string. Warning: `array` is modified in place if ``list``.

Example

array = [1, 2, 3, 4] splice(array, 1)

Expected output: [2, 3, 4]

Alternative usage:

array

Expected output: [1]

Source Code

def splice( array: Union[List[T], str], start: int, delete_count: Optional[int] = None, *items: Any, ) -> Union[List[T], str]: # List handling if isinstance(array, list): length = len(array) s = start if start >= 0 else max(length + start, 0) d = delete_count if delete_count is not None else length - s removed = array[s : s + d] array[s : s + d] = list(items) return removed # String handling text: str = array # type: ignore length = len(text) s = start if start >= 0 else max(length + start, 0) d = delete_count if delete_count is not None else length - s return text[:s] + "".join(items) + text[s + d :]