I feel like there's probably a smarter way to approach this
particular situation, but without any context, it's difficult for
me advise without making too many assumptions.
So for now, the best I can give you is a different approach.
It's potentially more efficient, but also perhaps less readable: