Skip to content

capturegraph.data.containers.utilities.enumerate #

enumerate - Enumerated List Access#

Enumerate a List, returning Dicts with index and value fields.

Example
from capturegraph.data import enumerate

# Get indexed access to photos
indexed = enumerate(sessions.photos)
# → List[Dict] with {index: 0, value: photo1}, {index: 1, value: photo2}, ...

# Create numbered filenames
filenames = indexed.map(lambda r: f"photo_{r.index:04d}.jpg")

enumerate(items, start=0) #

Enumerate a List, returning Dicts with index and value.

Parameters:

Name Type Description Default
items List[T]

The List to enumerate.

required
start int

The starting index (default 0).

0

Returns:

Type Description
List[Dict]

List of Dicts, each with index and value fields.

Example
photos = List([Path("a.jpg"), Path("b.jpg"), Path("c.jpg")])
enumerate(photos)
# List([
#     Dict({'index': 0, 'value': Path('a.jpg')}),
#     Dict({'index': 1, 'value': Path('b.jpg')}),
#     Dict({'index': 2, 'value': Path('c.jpg')})
# ])

enumerate(photos).map(lambda r: f"photo_{r.index:04d}.jpg")
# List(['photo_0000.jpg', 'photo_0001.jpg', 'photo_0002.jpg'])
Source code in capturegraph-lib/capturegraph/data/containers/utilities/enumerate.py
def enumerate[T](items: List[T], start: int = 0) -> List[Dict]:
    """Enumerate a List, returning Dicts with index and value.

    Args:
        items: The List to enumerate.
        start: The starting index (default 0).

    Returns:
        List of Dicts, each with ``index`` and ``value`` fields.

    Example:
        ```python
        photos = List([Path("a.jpg"), Path("b.jpg"), Path("c.jpg")])
        enumerate(photos)
        # List([
        #     Dict({'index': 0, 'value': Path('a.jpg')}),
        #     Dict({'index': 1, 'value': Path('b.jpg')}),
        #     Dict({'index': 2, 'value': Path('c.jpg')})
        # ])

        enumerate(photos).map(lambda r: f"photo_{r.index:04d}.jpg")
        # List(['photo_0000.jpg', 'photo_0001.jpg', 'photo_0002.jpg'])
        ```
    """
    return List(
        [
            Dict({"index": i, "value": item})
            for i, item in _builtin_enumerate(items, start=start)
        ]
    )