Iteration Classes
itertools.groupby()
Group consecutive items with equal keys; nonadjacent equal keys form separate groups. Group iterators share the source, so consume each before advancing.
Formal Syntax & Structure
python syntaxpython
itertools.groupby(iterable, key=None)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| iterable | iterable | Input values, consumed as needed. |
| key | callable or None | Optional key function used for comparison or grouping. |
Return Value
iterator
The iterator result described above.
Working Example
Example Snippetpython
import itertoolsprint([(k,list(g)) for k,g in itertools.groupby("AABA")])