Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Altering duplicate values from given list

Python | Altering duplicate values from given list

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




# Python3 code to demonstrate
# Altering duplicated
# using list comprehension + enumerate()
 
# initializing list
test_list = [2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + enumerate()
# Altering duplicated
res = [False if (ele in test_list[ :idx]) else ele
             for idx, ele in enumerate(test_list)]
 
# print result
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




# Python3 code to demonstrate
# Altering duplicated
# using itertools.groupby() + list comprehension
from itertools import groupby
 
# initializing list
test_list = [2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using itertools.groupby() + list comprehension
# Altering duplicated
res = [val for key, grp in groupby(test_list)
       for val in [key] + [False] * (len(list(grp))-1)]
 
# print result
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




# Initialize the list of values
test_list = [2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5]
 
# Print the original list
print("Original list:", test_list)
 
# Initialize an empty result list and a dictionary to track seen values
res = []
seen = {}
 
# Iterate through the list of values
for ele in test_list:
    # If the value has not been seen before, append it to the result list
    # and add it to the dictionary
    if ele not in seen:
        res.append(ele)
        seen[ele] = True
    # If the value has been seen before, append False to the result list
    else:
        res.append(False)
 
# Print the altered list
print("Altered list:", res)
#This code is contributed by Edula Vinay Kumar Reddy


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
 
# Initializing list
test_list = [2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Creating numpy array
arr = np.array(test_list)
 
# Getting unique values and their indices
unique, indices = np.unique(arr, return_index=True)
 
# Creating a boolean array with the same size as input list
res = np.zeros_like(arr, dtype=bool)
 
# Setting the first occurrence of each unique value to True
res[indices] = True
res = np.where(res, arr, False)
res = [False if val == 0 else val for val in res] # replace 0 with False
 
# Printing the altered duplicate list
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments