gh-38390: simplify some iterations (ruff PERF102)
this is simplifying some iterations, and was scripted using `ruff`
### :memo: Checklist
- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
URL: https://github.com/sagemath/sage/pull/38390
Reported by: Frédéric Chapoton
Reviewer(s): David Coudert