- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to Mark duplicate elements in string
When it is required to mark duplicate elements in a string, list comprehension along with the ‘count’ method is used.
Example
Below is a demonstration of the same
my_list = ["python", "is", "fun", "python", "is", "fun", "python", "fun"] print("The list is :") print(my_list) my_result = [value + str(my_list[:index].count(value) + 1) if my_list.count(value) > 1 else value for index, value in enumerate(my_list)] print("The result is :") print(my_result)
Output
The list is : ['python', 'is', 'fun', 'python', 'is', 'fun', 'python', 'fun'] The result is : ['python1', 'is1', 'fun1', 'python2', 'is2', 'fun2', 'python3', 'fun3']
Explanation
A list is defined and is displayed on the console.
The list comprehension is used to iterate through the values and check the count.
If the count of a specific value is greater than 1, the value is added to the element’s count.
Otherwise, it is enumerated over.
This is assigned to a variable.
It is the output that is displayed on the console.
- Related Articles
- Python program to find all duplicate characters in a string
- Python program to print the duplicate elements of an array
- Python – Insert character in each duplicate string after every K elements
- Program to find string after removing consecutive duplicate characters in Python
- Program to remove duplicate characters from a given string in Python
- Program to find duplicate item from a list of elements in Python
- Python program to remove duplicate elements from a Circular Linked List
- Program to find string after deleting k consecutive duplicate characters in python
- Program to find duplicate elements and delete last occurrence of them in Python
- Python program to remove duplicate elements from a Doubly Linked List\n
- Java Program to Remove duplicate elements from ArrayList
- Python - Replace duplicate Occurrence in String
- Java Program to find duplicate characters in a String?
- C Program to delete the duplicate elements in an array
- Write a program in Python to check if a series contains duplicate elements or not

Advertisements