0
# Summarizing Operations
1
2
Functions that compute summaries and statistics over iterables.
3
4
## Capabilities
5
6
### Length and Count Operations
7
8
Functions for measuring iterables and counting elements.
9
10
```python { .api }
11
def ilen(iterable: Iterable[Any]) -> int: ...
12
def quantify(iterable: Iterable[Any], pred: Callable[[Any], bool] = bool) -> int: ...
13
```
14
15
**Usage:**
16
17
```python
18
from more_itertools import ilen, quantify
19
20
# Count elements in an iterable
21
data = range(100)
22
count = ilen(data) # 100
23
24
# Count elements matching predicate
25
numbers = [1, 2, 3, 4, 5, 6]
26
even_count = quantify(numbers, lambda x: x % 2 == 0) # 3
27
```
28
29
### Equality and Uniqueness Checks
30
31
Functions for testing properties across all elements.
32
33
```python { .api }
34
def all_equal(iterable: Iterable[Any]) -> bool: ...
35
def all_unique(iterable: Iterable[Any]) -> bool: ...
36
```
37
38
**Usage:**
39
40
```python
41
from more_itertools import all_equal, all_unique
42
43
# Check if all elements are equal
44
all_equal([1, 1, 1, 1]) # True
45
all_equal([1, 2, 1, 1]) # False
46
47
# Check if all elements are unique
48
all_unique([1, 2, 3, 4]) # True
49
all_unique([1, 2, 2, 4]) # False
50
```
51
52
### Min/Max Operations
53
54
Functions for finding extremes and their positions.
55
56
```python { .api }
57
def minmax(iterable: Iterable[Any], *, default=None, key: Callable[[Any], Any] = None) -> tuple[Any, Any]: ...
58
def argmax(iterable: Iterable[Any], *, default=None, key: Callable[[Any], Any] = None) -> Any: ...
59
def argmin(iterable: Iterable[Any], *, default=None, key: Callable[[Any], Any] = None) -> Any: ...
60
```
61
62
**Usage:**
63
64
```python
65
from more_itertools import minmax, argmax, argmin
66
67
data = [3, 1, 4, 1, 5, 9, 2]
68
69
# Get both min and max in one pass
70
min_val, max_val = minmax(data) # (1, 9)
71
72
# Get index of maximum value
73
max_index = argmax(data) # 5 (index of value 9)
74
75
# Get index of minimum value
76
min_index = argmin(data) # 1 (index of first occurrence of 1)
77
```
78
79
### Run-Length Encoding
80
81
Class for compressing and decompressing consecutive identical elements.
82
83
```python { .api }
84
class run_length:
85
"""Run-length encoding and decoding operations."""
86
87
@staticmethod
88
def encode(iterable):
89
"""
90
Compress iterable with run-length encoding.
91
92
Args:
93
iterable: The iterable to compress
94
95
Returns:
96
Iterator of (item, count) tuples
97
"""
98
99
@staticmethod
100
def decode(iterable):
101
"""
102
Decompress run-length encoded data.
103
104
Args:
105
iterable: Iterator of (item, count) tuples
106
107
Returns:
108
Iterator of decompressed items
109
"""
110
```
111
112
**Usage:**
113
114
```python
115
from more_itertools import run_length
116
117
# Encode runs of identical elements
118
data = [1, 1, 1, 2, 2, 3, 3, 3, 3]
119
encoded = list(run_length.encode(data)) # [(1, 3), (2, 2), (3, 4)]
120
121
# Decode back to original
122
decoded = list(run_length.decode(encoded)) # [1, 1, 1, 2, 2, 3, 3, 3, 3]
123
```