{"id":124893,"date":"2022-05-17T14:21:18","date_gmt":"2022-05-17T14:21:18","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=365014"},"modified":"2022-05-17T14:21:18","modified_gmt":"2022-05-17T14:21:18","slug":"5-best-ways-to-check-a-list-for-duplicates-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/05\/17\/5-best-ways-to-check-a-list-for-duplicates-in-python\/","title":{"rendered":"5 Best Ways to Check a List for Duplicates in Python"},"content":{"rendered":"<div class=\"kk-star-ratings kksr-valign-top kksr-align-left \" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;365014&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;0&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\"> <span class=\"kksr-muted\">Rate this post<\/span> <\/div>\n<\/div>\n<h2 class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">Problem Formulation and Solution Overview<\/h2>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">In this article, you&#8217;ll learn how to check a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">List<\/a> for Duplicates in Python.<\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">To make it more fun, we have the following running scenario:<\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\"><em>The <a rel=\"noreferrer noopener\" href=\"https:\/\/academy.finxter.com\/\" data-type=\"URL\" data-id=\"https:\/\/academy.finxter.com\/\" target=\"_blank\">Finxter Academy<\/a> has given you an extensive list of usernames. Somewhere along the line, duplicate entries were added. They need you to check if their <em><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> <\/em>contains duplicates. For testing purposes, a small sampling of this <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> is used.<\/em><\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background\"><em><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How would we write Python code to check a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> for duplicate elements?<\/em><\/p>\n<p class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">We can accomplish this task by one of the following options:<\/p>\n<ul type=\"video\" class=\"wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<li><strong>Method 1<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> to return a <strong>Duplicate-Free<\/strong> <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a><\/li>\n<li><strong>Method 2<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>For<\/code><\/a> loop and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> to return a List of <strong>Duplicates<\/strong> found.<\/li>\n<li><strong>Method 3<\/strong>: Use a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>For<\/code><\/a> loop to return <strong>Duplicates<\/strong> and <strong>Counts<\/strong><\/li>\n<li><strong>Method 4<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-any-function\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-any-function\/\" target=\"_blank\"><code>any()<\/code><\/a> to check for <strong>Duplicates<\/strong> and return a Boolean<\/li>\n<li><strong>Method 5<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a> to return a List of all <strong>Duplicates <\/strong><\/li>\n<\/ul>\n<hr class=\"wp-block-separator wp-embed-aspect-16-9 wp-has-aspect-ratio\"\/>\n<h2>Method 1: Use set() and List to return a Duplicate-Free List<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a>which removes any duplicate values (<code>set(users)<\/code>) to produce a <strong>Duplicate-Free<\/strong> <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a>. This set is then converted to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">List<\/a> (<code>list(set(users))<\/code>). <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3', 'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek'] dup_free = list(set(users))\nprint(dup_free)<\/pre>\n<p>This code declares a small sampling of <a rel=\"noreferrer noopener\" href=\"https:\/\/finxter.com\/\" data-type=\"URL\" data-id=\"https:\/\/finxter.com\/\" target=\"_blank\">Finxter<\/a> usernames and saves them to <code>users<\/code>.<\/p>\n<p>Next, <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a> is called and <code>users<\/code> is passed as an argument to the same. Then, the new set is converted to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> and saved to <code>dup_free<\/code>.<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Python set() Function \u2014 A Simple Guide\" width=\"780\" height=\"439\" src=\"https:\/\/www.youtube.com\/embed\/fZJsKQPlzRg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<p>If <code><code>dup_free<\/code><\/code> was output to the terminal before converting to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a>, the result would be a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a>, which is <em>not subscriptable<\/em>. Meaning the elements are inaccessible in this format.<\/p>\n<p><strong>Output<\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>{'csealker', 'cdriver', 'shoeguy', 'ollie3', 'kyliek', 'stewieboy', 'AmyP'}<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note:<\/strong> Any attempt to access an element from a set will result in a <em>not subscriptable<\/em> error. <\/p>\n<p>In this example, the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a> was converted to a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\"><code>List<\/code><\/a>, and displays a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\"><code>List<\/code><\/a> of <strong>Duplicate-Free<\/strong> values.<\/p>\n<p><strong>Output <\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['csealker', 'cdriver', 'shoeguy', 'ollie3', 'kyliek', 'stewieboy', 'AmyP']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.1.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><strong>Note:<\/strong> An <em>empty<\/em> set will result if no argument is passed.<\/p>\n<hr class=\"wp-block-separator\"\/>\n<h2>Method 2: Use set(), For loop, and List to return a List of Duplicates Found<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a>, and a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\"><code>For<\/code><\/a> loop to check for and return any <strong>Duplicates<\/strong> found <code>(set(x for x in users if ((x in tmp) or tmp.add(x)))<\/code>) to <code>dups<\/code>. The <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a> is then converted to a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\"><code>List<\/code><\/a> (<code>print(list(dups))<\/code>).<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4-6\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3', 'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek'] tmp = set()\ndups = set(x for x in users if (x in tmp or tmp.add(x)))\nprint(list(dups))<\/pre>\n<p>This code declares a small sampling of <a rel=\"noreferrer noopener\" href=\"https:\/\/finxter.com\/\" data-type=\"URL\" data-id=\"https:\/\/finxter.com\/\" target=\"_blank\">Finxter<\/a> usernames and saves them to <code>users<\/code>.<\/p>\n<p>Next, a new empty set, <code>tmp<\/code> is declared. A <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\"><code>For<\/code><\/a> loop is then instantiated to check each element in <code>users<\/code> for duplicates. If a <strong>duplicate<\/strong> is found, it is appended to <code>tmp<\/code>. The results save to <code>dups<\/code> as a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a>.<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>In this example, the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\"><code>set()<\/code><\/a> was converted to a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\"><code>List<\/code><\/a> and displays a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\"><code>List<\/code><\/a> of Duplicates values found in the original <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\"><code>List<\/code><\/a>, <code>users<\/code>.<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code> ['kyliek', 'ollie3', 'shoeguy']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator\"\/>\n<h2>Method 3: Use a <code>For<\/code> loop to return <strong>Duplicates<\/strong> and <strong>Counts<\/strong><\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\"><code>For<\/code><\/a> loop to navigate through and check each element of <code>users<\/code> while keeping track of all usernames and the number of times they appear. A <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> of <strong>Duplicates<\/strong>, including the <strong>Usernames<\/strong> and <strong>Counts<\/strong> returns.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">count = {}\ndup_count = {}\nfor i in users: if i not in count: count[i] = 1 else: count[i] += 1 dup_count[i] = count[i]\nprint(dup_count)<\/pre>\n<p>This code declares two (2) empty sets, <code>count<\/code> and <code>dup_count<\/code> respectively.<\/p>\n<p>A <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-loops\/\" target=\"_blank\"><code>For<\/code><\/a> loop is instantiated to loop through each element of <code>users<\/code> and does the following:<\/p>\n<ul>\n<li>If the element <code>i<\/code> is not in <code>count<\/code>, then the <code>count<\/code> element (<code>count[i]=1<\/code>) is set to one (1).<\/li>\n<li>If element <code>i<\/code> is found in <code>count<\/code>, it falls to <code>else<\/code> where one (1) is added to <code>count<\/code> (<code>count[i]+=1<\/code>) and then added to <code>dup_count<\/code> (<code>dup_count[i]=count[i]<\/code>)<\/li>\n<\/ul>\n<p>This code repeats until the end of <code>users<\/code> has been reached.<\/p>\n<p>At this point, a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Dictionary<\/a> containing the <strong>Duplicates<\/strong>, and the number of times they appear displays.<\/p>\n<p><strong>Output<\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>{'ollie3': 2, 'shoeguy': 2, 'kyliek': 2}<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator\"\/>\n<h2>Method 4: Use Any to Check for Duplicate Values<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This example uses <code><a href=\"https:\/\/blog.finxter.com\/python-any-function\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-any-function\/\" target=\"_blank\" rel=\"noreferrer noopener\">any()<\/a><\/code>, and passes the <a href=\"https:\/\/blog.finxter.com\/iterators-iterables-and-itertools\/\" data-type=\"post\" data-id=\"29507\" target=\"_blank\" rel=\"noreferrer noopener\">iterable<\/a> <code>users<\/code> to iterate and locate <strong>Duplicates<\/strong>. If found, <code>True<\/code> returns. Otherwise, <code>False<\/code> returns. Best used on small <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">Lists<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3', 'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek'] dups = any(users.count(x) > 1 for x in users)\nprint(dups)<\/pre>\n<p>This code declares a small sampling of <a rel=\"noreferrer noopener\" href=\"https:\/\/finxter.com\/\" data-type=\"URL\" data-id=\"https:\/\/finxter.com\/\" target=\"_blank\">Finxter<\/a> usernames and saves them to <code>users<\/code>.<\/p>\n<p>Next, <a href=\"https:\/\/blog.finxter.com\/python-any-function\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-any-function\/\"><code>any()<\/code><\/a> is called and loops through each element of <code>users<\/code> checking to see if the element is a <strong>duplicate<\/strong>. If found, <code>True<\/code> is assigned. Otherwise, <code>False<\/code><em> <\/em>is assigned. The result saves to <code>dups<\/code> and the output displays as follows:<\/p>\n<p><strong>Output<\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td>True<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Python Built-in Functions - all() and any()\" width=\"780\" height=\"585\" src=\"https:\/\/www.youtube.com\/embed\/F6W3-N1-NtE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<hr class=\"wp-block-separator\"\/>\n<h2>Method 5: Use List Comprehension to return a <strong>List of all Duplicates<\/strong><\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This method uses <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a> to loop through <code>users<\/code>, checking for duplicates. If found, the <strong>Duplicates<\/strong> are appended to <code>dups<\/code>. <\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3', 'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek'] dups = [x for x in users if users.count(x) >= 2]\nprint(dups)<\/pre>\n<p>This code declares a small sampling of <a rel=\"noreferrer noopener\" href=\"https:\/\/finxter.com\/\" data-type=\"URL\" data-id=\"https:\/\/finxter.com\/\" target=\"_blank\">Finxter<\/a> usernames and saves them to <code>users<\/code>.<\/p>\n<p>Next, <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a> extracts and displays <strong>duplicate<\/strong> usernames and save them to a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">List<\/a>. The <strong>duplicate<\/strong> values are output to the terminal<\/p>\n<p><strong>Output<\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><code>['ollie3', 'shoeguy', 'kyliek', 'ollie3', 'shoeguy', 'kyliek']<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr class=\"wp-block-separator\"\/>\n<h2>Summary<\/h2>\n<p>These five (5) methods of checking a List for Duplicates should give you enough information to select the best one for your coding requirements.<\/p>\n<p>Good Luck &amp; Happy Coding!<\/p>\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Rate this post Problem Formulation and Solution Overview In this article, you&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-124893","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/124893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=124893"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/124893\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=124893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=124893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=124893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}