Open In App

Python – Replace all repeated occurrences with N

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Python3




# Python3 code to demonstrate
# Replace all repeated occurrences of K with N
# using enumerate() + set() + loop
 
# Initializing list
test_list = [1, 3, 3, 1, 4, 4, 1, 5, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing N
N = 'rep'
 
# Replace all repeated occurrences of K with N
# using enumerate() + set() + loop
his = set([])
for idx, ele in enumerate(test_list):
    if ele in his:
        test_list[idx] = N
    his.add(ele)
 
# printing result
print ("The duplication altered list : " + str(test_list))


Output : 

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']

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list

Method #2 : Using slicing and count() methods

Approach

  1. Keep the first occurrence of any element as it is
  2. And then the next occurrences are replaced by N

Python3




# Python3 code to demonstrate
# Replace all repeated occurrences of K with N
 
# Initializing list
test_list = [1, 3, 3, 1, 4, 4, 1, 5, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing N
N = 'rep'
res=[]
# Replace all repeated occurrences of K with N
for i in range(0,len(test_list)):
    if(test_list[:i+1].count(test_list[i])==1):
        res.append(test_list[i])
    else:
        res.append(N)
# printing result
print ("The duplication altered list : " + str(res))


Output

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']

Time Complexity : O(N)

Auxiliary Space : O(N)

Method #3: Using dictionary

Loops over each element in the list, and if the element has only one occurrence so far, it appends it to the result list. If the element is repeated, it appends the new value ‘N’ to the result list. Finally, the result list is printed.

Python3




test_list = [1, 3, 3, 1, 4, 4, 1, 5, 5]
 
# initializing dictionary to keep track of counts
count_dict = {}
 
# Replace all repeated occurrences of K with N
res = []
for num in test_list:
    if num in count_dict:
        res.append('rep')
    else:
        count_dict[num] = 1
        res.append(num)
 
print("The original list is : " + str(test_list))
print("The duplication altered list : " + str(res))


Output

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']

Time complexity: (n) because we iterate through the list once,
Auxiliary space: O(k) where k is the number of unique elements in the list because we store the counts in a dictionary.

Method #4 : Using slicing and operator.countOf() methods

Approach

  1. Keep the first occurrence of any element as it is(checked using operator.
  2. And then the next occurrences are replaced by N

Python3




# Python3 code to demonstrate
# Replace all repeated occurrences of K with N
 
# Initializing list
test_list = [1, 3, 3, 1, 4, 4, 1, 5, 5]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing N
N = 'rep'
res=[]
import operator
# Replace all repeated occurrences of K with N
for i in range(0,len(test_list)):
    if(test_list[:i+1].count(test_list[i])==1):
        res.append(test_list[i])
    else:
        res.append(N)
# printing result
print ("The duplication altered list : " + str(res))


Output

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']

Time Complexity : O(N) N – length of test_list

Auxiliary Space : O(N) N – length of output list (res)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads