summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2025-09-23 08:27:26 -0500
committerJohn Estabrook <jestabro@vyos.io>2025-09-23 08:27:26 -0500
commit6f4ce1ed5cd57d9b42617871389f68771f0e69e0 (patch)
treea41d671bae7be0293879bd3f5a2fbb018ae6865c /python
parent5bb78168ee43bad1ad93b3353b4753ae94aa3bd5 (diff)
downloadvyos-1x-6f4ce1ed5cd57d9b42617871389f68771f0e69e0.tar.gz
vyos-1x-6f4ce1ed5cd57d9b42617871389f68771f0e69e0.zip
T7850: add util list_strip to remove initial or final sub-list
Diffstat (limited to 'python')
-rw-r--r--python/vyos/utils/list.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/python/vyos/utils/list.py b/python/vyos/utils/list.py
index 931084e7c..ea4bcd3ba 100644
--- a/python/vyos/utils/list.py
+++ b/python/vyos/utils/list.py
@@ -18,3 +18,26 @@ def is_list_equal(first: list, second: list) -> bool:
if len(first) != len(second) or len(first) == 0:
return False
return sorted(first) == sorted(second)
+
+
+def list_strip(lst: list, sub: list, right: bool = False) -> list:
+ """Remove list 'sub' from beginning (right=False), resp., end of list 'lst'"""
+
+ if not right:
+ while sub:
+ if lst[:1] == sub[:1]:
+ lst = lst[1:]
+ sub = sub[1:]
+ else:
+ lst = []
+ sub = []
+ else:
+ while sub:
+ if lst[-1:] == sub[-1:]:
+ lst = lst[:-1]
+ sub = sub[:-1]
+ else:
+ lst = []
+ sub = []
+
+ return lst