Improved iteration logic over nonzero indices (using generator)