---
sidebar:
  label: roboto.collection_utils
  order: 6
title: roboto.collection_utils
---
## Module Contents

### T

```python
roboto.collection_utils.T
```

`from roboto.collection_utils import T`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/collection_utils.py#L11-L11)

Type of the values a `defaultlist` holds.

### defaultlist

```python
class roboto.collection_utils.defaultlist(factory: Callable[[], T])
```

`from roboto.collection_utils import defaultlist`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/collection_utils.py#L42-L93)

Bases: `list[T]`, `Generic[T]`

List that extends itself to any non-negative index read or written, filling new positions from a factory.

It works as `collections.defaultdict` does for keys: reading or writing an index past the end appends a value from `factory` for each missing position, then reads or writes that index, instead of raising `IndexError`. `factory` is called once per new position, so mutable values such as dictionaries are not shared between positions. Negative indexes and slices behave as they do on a `list`.

**Usage**

```python
dl = defaultlist[int](factory=lambda: 0)
dl[5] += 1
print(dl)
# [0, 0, 0, 0, 0, 1]
```

**Parameters**

- **factory** (`Callable[[], T]`)

**Attributes**

- **defaultlist.factory**: Function that takes no arguments and returns the value for a new position.

### get_by_path()

```python
def roboto.collection_utils.get_by_path(
    target: collections.abc.Mapping[Any, Any],
    key_path: collections.abc.Sequence[Any],
) -> Any
```

`from roboto.collection_utils import get_by_path`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/collection_utils.py#L15-L39)

Return the value at the end of a path of keys through nested mappings.

**Parameters**

- **target** (`collections.abc.Mapping[Any, Any]`): Mapping to read from.
- **key_path** (`collections.abc.Sequence[Any]`): Keys to follow, outermost first. An empty sequence returns `target` itself.

**Returns**

- `Any`: The value at the end of the path, or `None` when a key along it is missing, unhashable, or reached through a value that is not a mapping. A `None` stored at the end of the path is returned the same way, so the result does not tell a missing key from a stored `None`.

**Usage**

```python
get_by_path({"error": {"error_code": "NOT_FOUND"}}, ["error", "error_code"])
# 'NOT_FOUND'
get_by_path({"error": "Not found"}, ["error", "error_code"]) is None
# True
```
