How to Identify and Index Duplicates in a List .Pythonic Tips & Tricks — Checking and Indexing Duplicates
Duplicate detecting and indexing is a fundamental skill every data scientist should have. When dealing with any dataset, it is important to identify and locate values that are identical. In this article, we shall take a look at several techniques you can use.
Let’s get started!
So let’s say you were given the below question.
“Create a function that would check if a list has duplicate values, if it does have duplicate values list them all. If the list has no duplicates return “No Duplicates”.
Fairly simple question, let us see how to code it.
def duplicates(example_list): return len(example_list) != len(set(example_list)) result_1 = duplicates(['1','2','3','5','5','6','6']) result_2 = duplicates(['1','2','3','5','6']) print(result_1) print(result_2)
The code above is able to check if the inputted list contains any duplicates. It will return ‘True’ if the list does indeed have duplicates and ‘False’ if it does not. However this only answers half of our question.
Let us find a way for it to return the values that are duplicated. To do this we can make use of an If-Else statement coupled with some list comprehension.
def duplicates_1(example_list): if (len(example_list) != len(set(example_list))) is False: return 'No Duplicates' else: return set([i for i in example_list if example_list.count(i) > 1]) result_1 = duplicates_1(['1','2','3','5','5','6','6']) result_2 = duplicates_1(['1','2','3','5','6']) print(result_1) print(result_2)
🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...
In Conversation With Dr Suman Sanyal, NIIT University,he shares his insights on how universities can contribute to this highly promising sector and what aspirants can do to build a successful data science career.
Enroll in our Data Science with Python training in Chennai. Best Data Science with Python Training courses in Chennai for 100% Job Placements Support.
🔥Intellipaat Python for Data Science Course: https://intellipaat.com/python-for-data-science-training/In this python for data science video you will learn e...
Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now