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
# 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
OutputOriginal 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.
Similar Reads
Python | Get duplicate tuples from list
Sometimes, while working with records, we can have a problem of extracting those records which occur more than once. This kind of application can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() + count() In
7 min read
Python - Remove Duplicates from a List
Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets
2 min read
Remove Duplicate Strings from a List in Python
Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values.Pythona = ["Learn", "Python", "With"
3 min read
Python | Sort given list by frequency and remove duplicates
Problems associated with sorting and removal of duplicates is quite common in development domain and general coding as well. The sorting by frequency has been discussed, but sometimes, we even wish to remove the duplicates without using more LOC's and in a shorter way. Let's discuss certain ways in
5 min read
Python | Duplicate substring removal from list
Sometimes we can come to the problem in which we need to deal with certain strings in a list that are separated by some separator and we need to remove the duplicates in each of these kinds of strings. Simple shorthands to solve this kind of problem is always good to have. Let's discuss certain ways
7 min read
How to Find Duplicates in a List - Python
Finding duplicates in a list is a common task in programming. In Python, there are several ways to do this. Letâs explore the efficient methods to find duplicates. Using a Set (Most Efficient for Large Lists)Set() method is used to set a track seen elements and helps to identify duplicates. Pythona
2 min read
Python | Find keys with duplicate values in dictionary
Given a dictionary, the task is to find keys with duplicate values. Let's discuss a few methods for the same. Method #1: Using Naive approach In this method first, we convert dictionary values to keys with the inverse mapping and then find the duplicate keys Python3 # Python code to demonstrate # fi
4 min read
Python - Remove duplicate words from Strings in List
Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The
6 min read
Python | Remove given element from the list
Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Python | Remove given character from Strings list
Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read