Python | Altering duplicate values from given list
Last Updated :
08 May, 2023
Many times we deal with the lists having identical numbers as a sequence and we wish to just keep the 1st occurrence of element and substituting all the occurrences with the different number. Let’s discuss certain ways in which this can be done.
Method #1 : Using list comprehension + enumerate() This task can be achieved using the list comprehension for traversal and checking for element occurrence and index checking can be done using enumerate function.
Python3
test_list = [ 2 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 5 , 5 , 5 ]
print ("The original list : " + str (test_list))
res = [ False if (ele in test_list[ :idx]) else ele
for idx, ele in enumerate (test_list)]
print ("The altered duplicate list is : " + str (res))
|
Output :
The original list : [2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5] The altered duplicate list is : [2, False, 3, False, False, False, 4, False, 5, False, False]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using list comprehension + enumerate() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list
Method #2 : Using itertools.groupby() + list comprehension This particular task can also be performed using a combination of above function, using groupby function to get the groups of different elements and list comprehension to alter duplicates.
Python3
from itertools import groupby
test_list = [ 2 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 5 , 5 , 5 ]
print ("The original list : " + str (test_list))
res = [val for key, grp in groupby(test_list)
for val in [key] + [ False ] * ( len ( list (grp)) - 1 )]
print ("The altered duplicate list is : " + str (res))
|
Output :
The original list : [2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5] The altered duplicate list is : [2, False, 3, False, False, False, 4, False, 5, False, False]
Method #3 : Using dictionary
Initialize an empty result list and a dictionary to track seen values. If the value has been seen before, append False to the result list
Python3
test_list = [ 2 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 5 , 5 , 5 ]
print ( "Original list:" , test_list)
res = []
seen = {}
for ele in test_list:
if ele not in seen:
res.append(ele)
seen[ele] = True
else :
res.append( False )
print ( "Altered list:" , res)
|
Output
Original list: [2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5]
Altered list: [2, False, 3, False, False, False, 4, False, 5, False, False]
Time complexity: O(n)
Auxiliary space: O(n)
Using numpy:
Algorithm:
- Convert the input list into a numpy array using np.array() function.
- Get the unique values and their indices from the array using np.unique() function with return_index=True.
- Create a boolean array with the same size as the input array using np.zeros_like() function with dtype=bool.
- Set the first occurrence of each unique value in the boolean array to True using the unique indices.
- Use np.where() function to replace the duplicate values with False in the original array using the boolean array.
- Convert the numpy array back to a list using .tolist() method and print the altered duplicate list.
Python3
import numpy as np
test_list = [ 2 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 5 , 5 , 5 ]
print ( "The original list : " + str (test_list))
arr = np.array(test_list)
unique, indices = np.unique(arr, return_index = True )
res = np.zeros_like(arr, dtype = bool )
res[indices] = True
res = np.where(res, arr, False )
res = [ False if val = = 0 else val for val in res]
print ( "The altered duplicate list is : " , res)
|
Output:
The original list : [2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5]
The altered duplicate list is : [2, False, 3, False, False, False, 4, False, 5, False, False]
Time complexity: O(nlogn), where n is the length of the input list. This is because np.unique() function has a time complexity of O(nlogn) in the worst case.
Auxiliary space: O(n), as we’re using additional space for the numpy array and the boolean array, both with the same size as the input list.
Share your thoughts in the comments
Please Login to comment...