Sometimes, while working with Python list, we can have a problem in which we need to replace an element with another. But one can have variations of these such as increase of number and keeping the first occurrence. This can have applications in various domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using
enumerate() + set() + loop
The combination of above functions can be used to perform this task. In this, we iterate the list and then store the 1st occurrence in set, the consecutive values are tested using in and replaced inplace.
The original list is : [1, 3, 3, 1, 4, 4, 1, 5, 5] The duplication altered list : [1, 3, 'rep', 'rep', 4, 'rep', 'rep', 5, 'rep']
- Python | Replace characters after K occurrences
- Python - Replace all occurrences of a substring in a string
- replace() in Python to replace a substring
- Python | Pandas Series.str.replace() to replace text in a series
- Python - Maximum N repeated Elements
- Second most repeated word in a sequence in Python
- Python - Distance between occurrences
- Python | Remove repeated sublists from given list
- Python - Retain records with N occurrences of K
- Python | Deleting all occurrences of character
- Python | All occurrences of substring in string
- Python | Remove elements of list that are repeated less than k times
- Find the first repeated word in a string in Python using Dictionary
- Python | Record Similar tuple occurrences
- Python | Count occurrences of a character in string
- Python | Remove all occurrences in nested list
- Python | Count occurrences of an element in a list
- Python | Count occurrences of an element in a Tuple
- Remove all the occurrences of an element from a list in Python
- Python - Characters Index occurrences in String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.