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 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)) |
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 #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 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)) |
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]
Recommended Posts:
- Python - Remove duplicate values across Dictionary Values
- Python | Find keys with duplicate values in dictionary
- Python - Remove duplicate values in dictionary
- Python - Assign values to Values List
- PyQt5 - Checking if the duplicate values are allowed or not in ComboBox
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Removing duplicate dicts in list
- Python | Remove unordered duplicate elements from a list
- Python | Duplicate substring removal from list
- Python | Duplicate element indices in list
- Python | Get duplicate tuples from list
- Python - Remove duplicate words from Strings in List
- Python - Cross List Sync on duplicate elements removal
- Python | Visualize missing values (NaN) values using Missingno Library
- Python - Extract Unique values dictionary values
- Python - Extract ith column values from jth column values
- Python - Extract values of Particular Key in Nested Values
- Python - Test for Even values dictionary values lists
- Python - Remove keys with Values Greater than K ( Including mixed values )
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

