ddp_utils.ulist

import ddp_utils.ulist

Order-preserving deduplication, predicate lookup and list chunking.

Examples

Use this public operation:

import ddp_utils.ulist
ddp_utils.ulist.unique(seq: Iterable[T]) → List[T]

Return a list containing the first occurrence of each distinct item.

Parameters

Name

Type

Description

seq

Iterable[T]

Iterable of hashable items; consumed once, without modifying it.

Returns

Type

Description

List[T]

New list in encounter order; an empty iterable returns [].

Raises

Exception

Description

TypeError

An item is unhashable, for example a dict or list.

Examples

Use this public operation:

result = ddp_utils.ulist.unique(seq=seq_value)
ddp_utils.ulist.find(seq: Iterable[T], pred: Callable[[T], bool]) → T | None

Return the first item for which pred(item) is truthy, or None.

Parameters

Name

Type

Description

seq

Iterable[T]

Iterable searched in order; generators are consumed until a match.

pred

Callable[[T], bool]

Callable tested once per visited item; its exceptions propagate.

Returns

Type

Description

T | None

The original matching object, including a falsy item such as 0; None for no match. A matched None cannot be distinguished from no match.

Examples

Use this public operation:

result = ddp_utils.ulist.find(seq=seq_value, pred=pred_value)
ddp_utils.ulist.chunk(seq: List[T], size: int) → List[List[T]]

Return consecutive slices of a list using a range step.

Parameters

Name

Type

Description

seq

List[T]

List to split; the input is not mutated and elements are not copied.

size

int

Positive chunk size. Zero raises; negative values return [].

Returns

Type

Description

List[List[T]]

A list of lists, with a possibly shorter last chunk; [] for empty input.

Raises

Exception

Description

ValueError

size is zero, even for an empty list.

TypeError

size is not accepted as a range step.

Examples

Use this public operation:

result = ddp_utils.ulist.chunk(seq=seq_value, size=size_value)