(Fixes CI) Replaced set comprehension with a generator
- [x] closes #31149
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

ping @WillAyd  Thanks!