From 90102488951ed05cfaf3d20acfba864f7cdfb61f Mon Sep 17 00:00:00 2001 From: John Speed Meyers Date: Sat, 3 Feb 2024 20:43:29 -0500 Subject: [PATCH] Speedup create_list_without_duplicates function The current implementation includes a relatively expensive operation to check if an element is in a list. This commmit introduces a set operation that is constant time. Signed-off-by: John Speed Meyers --- src/spdx_tools/spdx/document_utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/spdx_tools/spdx/document_utils.py b/src/spdx_tools/spdx/document_utils.py index c0c0f5b27..f53784ad4 100644 --- a/src/spdx_tools/spdx/document_utils.py +++ b/src/spdx_tools/spdx/document_utils.py @@ -49,9 +49,12 @@ def create_document_without_duplicates(document: Document) -> Document: def create_list_without_duplicates(list_with_potential_duplicates: List[Any]) -> List[Any]: + seen_elements = set() list_without_duplicates = [] + for element in list_with_potential_duplicates: - if element not in list_without_duplicates: + if element not in seen_elements: + seen_elements.add(element) list_without_duplicates.append(element) return list_without_duplicates