diff --git a/unstructured/partition/common/common.py b/unstructured/partition/common/common.py index d18fc8c87b..4d10c78acc 100644 --- a/unstructured/partition/common/common.py +++ b/unstructured/partition/common/common.py @@ -338,7 +338,8 @@ def exactly_one(**kwargs: Any) -> None: Example: >>> exactly_one(filename=filename, file=file, text=text, url=url) """ - if sum([(arg is not None and arg != "") for arg in kwargs.values()]) != 1: + # Use generator expression to avoid creating a temporary list, reducing memory use + if sum((arg is not None and arg != "") for arg in kwargs.values()) != 1: names = list(kwargs.keys()) if len(names) > 1: message = f"Exactly one of {', '.join(names[:-1])} and {names[-1]} must be specified." diff --git a/unstructured/staging/base.py b/unstructured/staging/base.py index aab1b1647f..738b077fbe 100644 --- a/unstructured/staging/base.py +++ b/unstructured/staging/base.py @@ -462,21 +462,11 @@ def filter_element_types( include_element_types=include_element_types, exclude_element_types=exclude_element_types, ) - - filtered_elements: list[Element] = [] if include_element_types: - for element in elements: - if type(element) in include_element_types: - filtered_elements.append(element) - - return filtered_elements + return [element for element in elements if type(element) in include_element_types] elif exclude_element_types: - for element in elements: - if type(element) not in exclude_element_types: - filtered_elements.append(element) - - return filtered_elements + return [element for element in elements if type(element) not in exclude_element_types] return list(elements)