Refactored the check for len to accessing the list from last index