Skip to content

vllm.v1.core.sched.utils

remove_all

remove_all(lst: list, items_to_remove: set) -> list

Remove all items from a list that are in the items_to_remove set.

This method optimizes for the common case of removing a single item, falling back to list comprehension for multiple items.

Parameters:

Name Type Description Default
lst list

The list to remove items from

required
items_to_remove set

Set of items to remove

required

Returns:

Type Description
list

Either the modified original list (for single item removal) or

list

a new list (for multiple item removal). Callers should use the

list

returned value.

Note

For single item removal, this modifies the original list in-place and returns it. For multiple items, it creates and returns a new list.

Source code in vllm/v1/core/sched/utils.py
def remove_all(lst: list, items_to_remove: set) -> list:
    """Remove all items from a list that are in the items_to_remove set.

    This method optimizes for the common case of removing a single item,
    falling back to list comprehension for multiple items.

    Args:
        lst: The list to remove items from
        items_to_remove: Set of items to remove

    Returns:
        Either the modified original list (for single item removal) or
        a new list (for multiple item removal). Callers should use the
        returned value.

    Note:
        For single item removal, this modifies the original list in-place
        and returns it. For multiple items, it creates and returns a new list.
    """
    if not items_to_remove:
        return lst

    if len(items_to_remove) == 1:
        # Fast path for single item removal (most common case)
        item = next(iter(items_to_remove))
        with contextlib.suppress(ValueError):
            lst.remove(item)
        return lst
    # For multiple items, use list comprehension
    return [item for item in lst if item not in items_to_remove]