def remove_longest_lines(path_to_file: str) -> None: with open(path_to_file, 'r+') as file: lines = list(map(lambda s: s.replace("\n", ""), file.readlines())) max_length = max(map(len, lines)) indices_of_longest_lines = [ i for i in range(len(lines)) if len(lines[i]) == max_length ] lines = [ lines[i] for i in range(len(lines)) if i not in indices_of_longest_lines ] file.seek(0) file.writelines(lines) file.truncate()