refactoring code for range(len())