![]() |
|
[Tut] 5 Best Ways to Check a List for Duplicates in Python - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] 5 Best Ways to Check a List for Duplicates in Python (/thread-99446.html) |
[Tut] 5 Best Ways to Check a List for Duplicates in Python - xSicKxBot - 05-23-2022 5 Best Ways to Check a List for Duplicates in Python Rate this post Problem Formulation and Solution Overview In this article, you’ll learn how to check a List for Duplicates in Python. To make it more fun, we have the following running scenario: The Finxter Academy has given you an extensive list of usernames. Somewhere along the line, duplicate entries were added. They need you to check if their [i]List contains duplicates. For testing purposes, a small sampling of this List is used.[/i] Question: How would we write Python code to check a List for duplicate elements?We can accomplish this task by one of the following options:
Method 1: Use set() and List to return a Duplicate-Free List This method uses Code: set()Code: set(users)Code: set()Code: list(set(users))Code: users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3', 'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek'] dup_free = list(set(users))
print(dup_free)This code declares a small sampling of Finxter usernames and saves them to Code: usersNext, Code: set()Code: usersCode: ListCode: dup_freeIf Code: dup_freeCode: ListCode: set()Output Code: {'csealker', 'cdriver', 'shoeguy', 'ollie3', 'kyliek', 'stewieboy', 'AmyP'} Note: Any attempt to access an element from a set will result in a not subscriptable error.In this example, the Code: set()Code: ListCode: ListOutput Code: ['csealker', 'cdriver', 'shoeguy', 'ollie3', 'kyliek', 'stewieboy', 'AmyP'] Note: An empty set will result if no argument is passed.Method 2: Use set(), For loop, and List to return a List of Duplicates Found This method uses Code: set()Code: ForCode: (set(x for x in users if ((x in tmp) or tmp.add(x)))Code: dupsCode: set()Code: ListCode: print(list(dups))Here’s an example: Code: users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3', 'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek'] tmp = set()
dups = set(x for x in users if (x in tmp or tmp.add(x)))
print(list(dups))This code declares a small sampling of Finxter usernames and saves them to Code: usersNext, a new empty set, Code: tmpCode: ForCode: usersCode: tmpCode: dupsCode: set()Output In this example, the Code: set()Code: ListCode: ListCode: ListCode: usersCode: ['kyliek', 'ollie3', 'shoeguy']Method 3: Use a Code: ForThis method uses a Code: ForCode: usersHere’s an example: Code: count = {}
dup_count = {}
for i in users: if i not in count: count[i] = 1 else: count[i] += 1 dup_count[i] = count[i]
print(dup_count)This code declares two (2) empty sets, Code: countCode: dup_countA Code: ForCode: users
This code repeats until the end of Code: usersAt this point, a Dictionary containing the Duplicates, and the number of times they appear displays. Output Code: {'ollie3': 2, 'shoeguy': 2, 'kyliek': 2}Method 4: Use Any to Check for Duplicate Values This example uses Code: any()Code: usersCode: TrueCode: FalseCode: users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3', 'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek'] dups = any(users.count(x) > 1 for x in users)
print(dups)This code declares a small sampling of Finxter usernames and saves them to Code: usersNext, Code: any()Code: usersCode: TrueCode: FalseCode: dupsOutput True Method 5: Use List Comprehension to return a [b]List of all Duplicates[/b] This method uses List Comprehension to loop through Code: usersCode: dupsHere’s an example: Code: users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3', 'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek'] dups = [x for x in users if users.count(x) >= 2]
print(dups)This code declares a small sampling of Finxter usernames and saves them to Code: usersNext, List Comprehension extracts and displays duplicate usernames and save them to a List. The duplicate values are output to the terminal Output Code: ['ollie3', 'shoeguy', 'kyliek', 'ollie3', 'shoeguy', 'kyliek']Summary These five (5) methods of checking a List for Duplicates should give you enough information to select the best one for your coding requirements. Good Luck & Happy Coding! |