This is a `groupby` operation.  This is implemented in `sift` (although maybe you already knew that).

```
from sympy.utilities.iterables import sift
keys_by_predicate = sift(keys, lambda key: key.func)
```