file_name = 'my_file.txt' my_list = [] prev_line = '' with open(file_name, 'r') as file: for line in file: if prev_line != line: my_list = [line] else: my_list.append(line) print(my_list) prev_line = line
some_text
some_text
some_text
other_text
other_text
other_text
another_text
another_text
another_text