| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import copy |
| 5 |
from typing import Any |
| 6 |
|
| 7 |
|
| 8 |
def without(needle: list[Any], container: list[Any]) -> list[Any]: |
| 9 |
""" |
| 10 |
Create a copy of a list with all occurences of sublist ``needle`` removed. |
| 11 |
""" |
| 12 |
if not needle: |
| 13 |
return copy.copy(container) |
| 14 |
|
| 15 |
needle_first = needle[0] |
| 16 |
needle_len = len(needle) |
| 17 |
|
| 18 |
start_at = 0 |
| 19 |
result = [] |
| 20 |
|
| 21 |
while True: |
| 22 |
|
| 23 |
try: |
| 24 |
found_at = container.index(needle_first, start_at) |
| 25 |
except ValueError: |
| 26 |
|
| 27 |
result.extend(container[start_at:]) |
| 28 |
break |
| 29 |
|
| 30 |
|
| 31 |
if container[found_at : found_at + needle_len] == needle: |
| 32 |
|
| 33 |
result.extend(container[start_at:found_at]) |
| 34 |
start_at = found_at + needle_len |
| 35 |
else: |
| 36 |
|
| 37 |
result.extend(container[start_at : found_at + 1]) |
| 38 |
start_at = found_at + 1 |
| 39 |
|
| 40 |
return result |