Python – Replace all repeated occurrences with N
Last Updated :
04 Apr, 2023
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
test_list = [ 1 , 3 , 3 , 1 , 4 , 4 , 1 , 5 , 5 ]
print ("The original list is : " + str (test_list))
N = 'rep'
his = set ([])
for idx, ele in enumerate (test_list):
if ele in his:
test_list[idx] = N
his.add(ele)
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
- Keep the first occurrence of any element as it is
- And then the next occurrences are replaced by N
Python3
test_list = [ 1 , 3 , 3 , 1 , 4 , 4 , 1 , 5 , 5 ]
print ( "The original list is : " + str (test_list))
N = 'rep'
res = []
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)
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 ]
count_dict = {}
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
- Keep the first occurrence of any element as it is(checked using operator.
- And then the next occurrences are replaced by N
Python3
test_list = [ 1 , 3 , 3 , 1 , 4 , 4 , 1 , 5 , 5 ]
print ( "The original list is : " + str (test_list))
N = 'rep'
res = []
import operator
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)
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)
Share your thoughts in the comments
Please Login to comment...