How to remove duplicates from list in python 3

How to remove duplicates from list in python 3 :

Hello friends, In this article, we will see How to remove duplicates items or elements from the list in python so Let’s start :
How to remove duplicates from list in python 3
How to remove duplicates from list in python 3
 
There are different way to remove duplicates from list :
  1. Method 1 : Using list comprehension method
  2. Method 2 : Using list comprehension + enumerate() method
  3. Method 3 : Using set() method
  4. Method 4 : Naive method
  5. Method 5 : Using collections.OrderedDict.fromkeys() method

Method 1 : Using list comprehension method

This method has working similar to the Naive method , but small difference is it just a liner shorthand of longer methods done with the help of lists comprehension method.

For example :

# Demonstrate Python 3 code 
# remove duplicated from list using list comprehension
#  first we initializing list 
use_list = [4, 3, 7, 6, 3, 5, 7, 1] 
print (“The main/original list is : ” +  str(use_list)) 
# then we use using list comprehension method  
# to remove from list  
rest = [] 
[rest.append(x) for x in use_list if x not in rest] 
  
# printing list output after removal  
print (“The unique list after removing dupli is follow: ” + str(rest)) 
Output :
The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1] 
The unique list after removing dupli is follow : [4, 3, 7, 6, 5, 1]
This time to learn different python terminologies in python.

Method 2 : Using list comprehension + enumerate()

list comprehension coupled with enumerate function can also achieve this task. It basically looks for already occurred elements and skips adding them. It preserves the list ordering.
# removing duplicated from list using list comprehension + enumerate() 
# first initializing list 
use_list = [4, 3, 7, 6, 3, 5, 7, 1] 
print (“The main/original list is : ” +  str(use_list)) 
# using list comprehension + enumerate() method to remove duplicated  
# from  the list  
rest = [i for n, i in enumerate(use_list) if i not in test_list[:n]]  
print (“TThe unique list after removing dupli is follow : ” + str(rest)) 
Output :
The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1] 
The unique list after removing dupli is follow: [4, 3, 7, 6, 5, 1]

Method 3 : Naive method

In this method (naive method), we simply append the first occurrence of the element in new existing list and ignore all the other occurrences of the list.
# Demonstrate Python program 
# remove duplicated element from list using list Naive method
#  first we initializing list 
use_list = [4, 3, 7, 6, 3, 5, 7, 1] 
print (“The main/original list is : ” +  str(use_list)) 
# then we use using list Naive method  
rest = [] 
for i in test_list: 
    if i not in rest: 
        rest.append(i) 
  
# printing output after removal  
print (“The unique list after removing dupli is follow: ” + str(rest)) 
Output :
The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1] 
The unique list after removing dupli is follow: [4, 3, 7, 6, 5, 1]

Method 4 : Using set() method

 set() method is the very popular way by in which the duplicated element are removed from the list.
Drawback: This method is that the ordered of the elements is lost in this particular method.
# use Python code to demonstrate  
# we removing duplicated from list using set() method
# first we initializing list 
use_list = [1, 5, 3, 6, 3, 5, 6, 1] 
print (“The main/original list is : ” +  str(use_list)) 
  
use_list = list(set(use_list)) 
  
# print list after removal  distorted ordering method
print (“The unique list after removing dupli is : ” + str(rest)) 
Output :
The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1] 
The unique list after removing dupli is follow: [4, 3, 7, 6, 5, 1]
Method 5 : Using collections.OrderedDict.fromkeys() method
This is super fastest method to achieve the different task. 
# use Python code to demonstrate  
# we removing duplicated from list using collections.OrderedDict.fromkeys() method
# first we initializing list 
use_list = [1, 5, 3, 6, 3, 5, 6, 1] 
print (“The main/original list is : ” +  str(use_list)) 
rest = list(OrderedDict.fromkeys(use_list)) 
# print list after removal collections.OrderedDict.fromkeys() method
print (“The unique list after removing dupli is : ” + str(rest)) 
Output :
The main/original list is : [4, 3, 7, 6, 3, 5, 7, 1] 
The unique list after removing dupli is follow: [4, 3, 7, 6, 5, 1]
Some FAQ ABOUT remove duplicates from list in python 3 :
How do I remove duplicates from a list in Python?
There are a different ways in which we can remove duplicates from a list in Python
  1. Method 1 : Using list comprehension method
  2. Method 2 : Using list comprehension + enumerate() method
  3. Method 3 : Using set() method
  4. Method 4 : Naive method
  5. Method 5 : Using collections.OrderedDict.fromkeys() method
How do I remove duplicate tuples from a list in Python?
Following methods are used for remove duplicate tuples from a list in Python
  1. filter_none. 
  2. chevron_right. 
  3. filter_none. 
  4. chevron_right.
How do I remove multiple elements from a list in Python?
By index range using del method we Remove Multiple elements from list 
Summary : In this article we saw the different methods for remove duplicates from list in python 3 so about this article you have any question then please comment below.
Tags: How to remove duplicates from list in python 3, python, python trending
If you want to learn seaborn in python practical then click on it.  
BEST OF LUCK!!!
pagarsach14@gmail.com

I am the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Comment