05-23-2022, 11:49 PM
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 useswhich removes any duplicate values () to produce a Duplicate-Free . This set is then converted to a List ().
This code declares a small sampling of Finxter usernames and saves them to.
Next, is called and is passed as an argument to the same. Then, the new set is converted to a and saved to .
If was output to the terminal before converting to a , the result would be a , which is not subscriptable. Meaning the elements are inaccessible in this format.
Output
Note: Any attempt to access an element from a set will result in a not subscriptable error.
In this example, the was converted to a , and displays a of Duplicate-Free values.
Output
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, and a loop to check for and return any Duplicates found ) to . The is then converted to a ().
Here’s an example:
This code declares a small sampling of Finxter usernames and saves them to.
Next, a new empty set, is declared. A loop is then instantiated to check each element in for duplicates. If a duplicate is found, it is appended to . The results save to as a .
Output
In this example, the was converted to a and displays a of Duplicates values found in the original , .
Method 3: Use a loop to return [b]Duplicates and Counts[/b]
This method uses a loop to navigate through and check each element of while keeping track of all usernames and the number of times they appear. A Dictionary of Duplicates, including the Usernames and Counts returns.
Here’s an example:
This code declares two (2) empty sets, and respectively.
A loop is instantiated to loop through each element of and does the following:
This code repeats until the end of has been reached.
At this point, a Dictionary containing the Duplicates, and the number of times they appear displays.
Output
Method 4: Use Any to Check for Duplicate Values
This example uses, and passes the iterable to iterate and locate Duplicates. If found, returns. Otherwise, returns. Best used on small Lists.
This code declares a small sampling of Finxter usernames and saves them to.
Next, is called and loops through each element of checking to see if the element is a duplicate. If found, is assigned. Otherwise, is assigned. The result saves to and the output displays as follows:
Output
True
Method 5: Use List Comprehension to return a [b]List of all Duplicates[/b]
This method uses List Comprehension to loop through, checking for duplicates. If found, the Duplicates are appended to .
Here’s an example:
This code declares a small sampling of Finxter usernames and saves them to.
Next, List Comprehension extracts and displays duplicate usernames and save them to a List. The duplicate values are output to the terminal
Output
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!
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 andCode:
set()to return a Duplicate-Free ListCode:List
- Method 2: Use ,Code:
set()loop andCode:Forto return a List of Duplicates found.Code:List
- Method 3: Use a loop to return Duplicates and CountsCode:
For
- Method 4: Use to check for Duplicates and return a BooleanCode:
any()
- Method 5: Use List Comprehension to return a List of all Duplicates
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- If the element is not inCode:
i, then theCode:countelement (Code:count) is set to one (1).Code:count[i]=1
- If element is found inCode:
i, it falls toCode:countwhere one (1) is added toCode:else(Code:count) and then added toCode:count[i]+=1(Code:dup_count)Code:dup_count[i]=count[i]
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!

