Skip to content

edsnlp.utils.collections

dedup(sequence, key=None)

Deduplicate a sequence, keeping the last occurrence of each item.

PARAMETER DESCRIPTION
sequence

Sequence to deduplicate

TYPE: Sequence

key

Key function to use for deduplication, by default None

TYPE: Callable, optional DEFAULT: None

Source code in edsnlp/utils/collections.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def dedup(sequence, key=None):
    """
    Deduplicate a sequence, keeping the last occurrence of each item.

    Parameters
    ----------
    sequence : Sequence
        Sequence to deduplicate
    key : Callable, optional
        Key function to use for deduplication, by default None
    """
    key = (lambda x: x) if key is None else key
    return list({key(item): item for item in sequence}.values())