Python program to Mark duplicate elements in string
Last Updated :
01 Feb, 2023
Given a list, the task is to write a Python program to mark the duplicate occurrence of elements with progressive occurrence number.
Input : test_list = ['gfg', 'is', 'best', 'gfg', 'best', 'for', 'all', 'gfg']
Output : ['gfg1', 'is', 'best1', 'gfg2', 'best2', 'for', 'all', 'gfg3']
Explanation : gfg's all occurrence are marked as it have multiple repetitions(3).
Input : test_list = ['gfg', 'is', 'best', 'best', 'for', 'all']
Output : ['gfg', 'is', 'best1', 'best2', 'for', 'all']
Explanation : best's all occurrence are marked as it have multiple repetitions(2).
Method 1: Using count() + enumerate() + list comprehension + slicing
In this, in order to get the duplicate count, the list is sliced to current element index, and count of occurrence of that element till current index is computed using count() and append.
Python3
# Python3 code to demonstrate working of
# Mark duplicate elements
# Using count() + enumerate() + list comprehension + slicing
# initializing list
test_list = ["gfg", "is", "best", "gfg",
"best", "for", "all", "gfg"]
# printing original list
print("The original list is : " + str(test_list))
# getting count till current using count() and slicing
res = [val + str(test_list[:idx].count(val) + 1) if test_list.count(val) > 1 else val for idx,
val in enumerate(test_list)]
# printing result
print("Duplicates marked List : " + str(res))
Output:
The original list is : ['gfg', 'is', 'best', 'gfg', 'best', 'for', 'all', 'gfg']
Duplicates marked List : ['gfg1', 'is', 'best1', 'gfg2', 'best2', 'for', 'all', 'gfg3']
Time Complexity: O(n)
Auxiliary Space : O(n)
Method 2: Using map() + count() + lambda
Similar to the above method, the only difference being map() is used to get a function using lambda to extend to whole list elements.
Python3
# Python3 code to demonstrate working of
# Mark duplicate elements
# Using map() + count() + lambda
# initializing list
test_list = ["gfg", "is", "best", "gfg",
"best", "for", "all", "gfg"]
# printing original list
print("The original list is : " + str(test_list))
# getting count till current using count() and slicing
res = list(map(lambda ele: ele[1] + str(test_list[ : ele[0]].count(ele[1]) + 1) if test_list.count(ele[1]) > 1 else ele[1],
enumerate(test_list)))
# printing result
print("Duplicates marked List : " + str(res))
Output:
The original list is : ['gfg', 'is', 'best', 'gfg', 'best', 'for', 'all', 'gfg']
Duplicates marked List : ['gfg1', 'is', 'best1', 'gfg2', 'best2', 'for', 'all', 'gfg3']
The Time and Space Complexity of all the methods is :
Time Complexity: O(n)
Space Complexity: O(n)
Method #3: Using operator.countOf() method
Python3
# Python3 code to demonstrate working of
# Mark duplicate elements
import operator as op
# initializing list
test_list = ["gfg", "is", "best", "gfg",
"best", "for", "all", "gfg"]
# printing original list
print("The original list is : " + str(test_list))
# getting count till current using count() and slicing
res = [val + str(op.countOf(test_list[:idx], val) + 1) if op.countOf(test_list, val) > 1 else val for idx,
val in enumerate(test_list)]
# printing result
print("Duplicates marked List : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best', 'gfg', 'best', 'for', 'all', 'gfg']
Duplicates marked List : ['gfg1', 'is', 'best1', 'gfg2', 'best2', 'for', 'all', 'gfg3']
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python program to find String in a List Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e
3 min read
Generate Random String Without Duplicates in Python When we need to create a random string in Python, sometimes we want to make sure that the string does not have any duplicate characters. For example, if we're generating a random password or a unique identifier, we might want to ensure each character appears only once. Using random.sample()Using ran
2 min read
Python | Removing duplicates from tuple Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set()
4 min read
Python | Remove duplicates in Matrix While working with Python Matrix, we can face a problem in which we need to perform the removal of duplicates from Matrix. This problem can occur in Machine Learning domain because of extensive usage of matrices. Let's discuss certain way in which this task can be performed. Method : Using loop This
2 min read
Python - Duplicate Element Indices in List We are having a list we need to find the duplicate element indices. For example, we are given a list a = [10, 20, 30, 20, 40, 30, 50] we need to find indices of the duplicate items so that output should be {20: [1, 3], 30: [2, 5]}.Using a loop and a dictionaryWe iterate through the list using enumer
3 min read
Python program to find Indices of Overlapping Substrings To count the number of overlapping sub-strings in Python we can use the Re module. To get the indices we will use the re.finditer() method. But it returns the count of non-overlapping indices only. Examples: Input: String: "geeksforgeeksforgeeks" ; Pattern: "geeksforgeeks" Output: [0, 8] Explanation
4 min read
Remove Unordered Duplicate Elements from a List - Python Given a list of elements, the task is to remove all duplicate elements from the list while maintaining the original order of the elements.For example, if the input is [1, 2, 2, 3, 1] the expected output is [1, 2, 3]. Let's explore various methods to achieve this in Python.Using setWe can initialize
3 min read
Python Program to Remove duplicate tuples irrespective of order Given a list of binary tuples, the task is to write a Python program to remove all tuples that are duplicates irrespective of order, i.e delete if contains similar elements, irrespective of order. Input : test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]Output : [(1, 2), (5, 7), (
6 min read
Python | Program to count duplicates in a list of tuples Given a list of tuples, write a Python program to check if an element of the list has duplicates. If duplicates exist, print the number of occurrences of each duplicate tuple, otherwise print "No Duplicates". Examples: Input : [('a', 'e'), ('b', 'x'), ('b', 'x'), ('a', 'e'), ('b', 'x')] Output : ('a
6 min read