you can use enumerate for this:

```
for i, bucket in enumerate(sorted(self.metric_by_bucket)):
```