stouputils.collections module — stouputils 1.0.18 documentation

# stouputils.collections module

This module provides utilities for collection manipulation:

- unique_list: Remove duplicates from a list while preserving order using object id, hash or str

unique_list(list_to_clean: list[Any], method: Literal['id', 'hash', 'str'] = 'str') → list[Any]

  Remove duplicates from the list while keeping the order using ids (default) or hash or str

  Parameters:

    - list_to_clean (list[Any]) – The list to clean

    - method (Literal["id", "hash", "str"]) – The method to use to identify duplicates

  Returns:

    The cleaned list

  Return type:

    list[Any]

  Examples

      >>> unique_list([1, 2, 3, 2, 1], method="id")
      [1, 2, 3]

      >>> s1 = {1, 2, 3}
      >>> s2 = {2, 3, 4}
      >>> s3 = {1, 2, 3}
      >>> unique_list([s1, s2, s1, s1, s3, s2, s3], method="id")
      [{1, 2, 3}, {2, 3, 4}, {1, 2, 3}]

      >>> s1 = {1, 2, 3}
      >>> s2 = {2, 3, 4}
      >>> s3 = {1, 2, 3}
      >>> unique_list([s1, s2, s1, s1, s3, s2, s3], method="str")
      [{1, 2, 3}, {2, 3, 4}]