# libtmux._internal.sparse_array.SparseArray

- **Module:** libtmux._internal.sparse_array
- **Package:** libtmux
- **Language:** Python
- **Kind:** class
- **Source:** https://github.com/tmux-python/libtmux/blob/38e368c11117fb4aeb2f082d552cd4f210eae06a/src/libtmux/_internal/sparse_array.py#L29
- **Bases:** [`dict`](https://docs.python.org/3/library/stdtypes.html#dict)`[`[`int`](https://docs.python.org/3/library/functions.html#int)`, `[`T`](/en/py/stable/reference/libtmux-_internal-sparse_array-sparsearray/#libtmux._internal.sparse_array.SparseArray.type-parameter.T)`]`, [`t.Generic`](https://docs.python.org/3/library/typing.html#typing.Generic)`[`[`T`](/en/py/stable/reference/libtmux-_internal-sparse_array-sparsearray/#libtmux._internal.sparse_array.SparseArray.type-parameter.T)`]`
- **Page:** https://libtmux.org/en/py/stable/reference/libtmux-_internal-sparse_array-sparsearray/

Support non-sequential indexes while maintaining :class:`list`-like behavior.

A normal :class:`list` would raise :exc:`IndexError`.

There are no native sparse arrays in python that contain non-sequential indexes and
maintain list-like behavior. This is useful for handling libtmux options and hooks:

[`command-alias[1] split-pane=split-window`](/en/tmux/latest/catalog/options/command-alias/) to
``{'command-alias[1]': {'split-pane=split-window'}}``

:class:`list` would lose indice info, and :class:`dict` would lose list-like
behavior.

## Examples

Create a sparse array and add values at non-sequential indices:

```python
>>> from libtmux._internal.sparse_array import SparseArray
```

```python
>>> arr: SparseArray[str] = SparseArray()
>>> arr.add(0, "first hook command")
>>> arr.add(5, "fifth hook command")
>>> arr.add(2, "second hook command")
```

Access values by index (dict-style):

```python
>>> arr[0]
'first hook command'
>>> arr[5]
'fifth hook command'
```

Check index existence:

```python
>>> 0 in arr
True
>>> 3 in arr
False
```

Iterate values in sorted index order:

```python
>>> list(arr.iter_values())
['first hook command', 'second hook command', 'fifth hook command']
```

Convert to a list (values only, sorted by index):

```python
>>> arr.as_list()
['first hook command', 'second hook command', 'fifth hook command']
```

Append adds at max index + 1:

```python
>>> arr.append("appended command")
>>> arr[6]
'appended command'
```

Access raw indices:

```python
>>> sorted(arr.keys())
[0, 2, 5, 6]
```

## Type parameters

- `T`

## Members

### Common operations

- [`add`](/en/py/stable/reference/libtmux-_internal-sparse_array-sparsearray-add/) (method): Add a value at a specific index.
- [`append`](/en/py/stable/reference/libtmux-_internal-sparse_array-sparsearray-append/) (method): Append a value at the next available index (max + 1).
- [`as_list`](/en/py/stable/reference/libtmux-_internal-sparse_array-sparsearray-as_list/) (method): Return values as a list in sorted index order.
- [`iter_values`](/en/py/stable/reference/libtmux-_internal-sparse_array-sparsearray-iter_values/) (method): Iterate over values in sorted index order.
