Advertisement
here2share

# remove_consecutive_duplicates.py

May 17th, 2025
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.39 KB | None | 0 0
  1. # remove_consecutive_duplicates.py
  2.  
  3. def remove_consecutive_duplicates(text):
  4.     lines = text.split("\n")  # Split the string into individual lines
  5.     filtered_lines = [lines[i] for i in range(len(lines)) if i == 0 or lines[i] != lines[i-1]]
  6.     return "\n".join(filtered_lines)
  7.  
  8. test = '''\
  9. 1
  10. 2
  11. 3
  12. 3
  13. 3
  14. 4
  15. 3
  16. 5
  17. 5
  18. 6
  19. '''
  20.  
  21. result = remove_consecutive_duplicates(test)
  22. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement