Something I've learned to do when I'm trying to "do it better" is to keep the "did it right but worse" method around as a checker of my work and then at the end of my routine put an assertion that the result is the same as the "worse" implementation. This helps to quickly get the bugs out of the better routine:

```
def ok(sparse):
    row, JA, A = [list(i) for i in zip(*sparse.row_list())]
    counts=defaultdict(int, group(row, multiple=False))
    IA=[0]
    for i in range(sparse.rows):
        IA.append(IA[-1] + counts[i])
    return [A, JA, IA]

def doktocsr(sparse):
    # better method
    rv = better's result
    assert rv = ok(sparse)
    return rv
```

Check the following:

```
>>> ok(SparseMatrix(Matrix(5,3,[0,0,0,0,2,0,0,0,0,0,0,3,0,0,0])))
[[2, 3], [1, 2], [0, 0, 1, 1, 2, 2]]
```