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 |
There are different way to remove duplicates from list :
- Method 1 : Using list comprehension method
- Method 2 : Using list comprehension + enumerate() method
- Method 3 : Using set() method
- Method 4 : Naive method
- 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.
# Demonstrate Python 3 code# remove duplicated from list using list comprehension# first we initializing listuse_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 listrest = [][rest.append(x) for x in use_list if x not in rest]# printing list output after removalprint ("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 listuse_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 listrest = [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 listuse_list = [4, 3, 7, 6, 3, 5, 7, 1]print ("The main/original list is : " + str(use_list))# then we use using list Naive methodrest = []for i in test_list:if i not in rest:rest.append(i)# printing output after removalprint ("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]
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 listuse_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 methodprint ("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 listuse_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() methodprint ("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.
- Method 1 : Using list comprehension method
- Method 2 : Using list comprehension + enumerate() method
- Method 3 : Using set() method
- Method 4 : Naive method
- 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
- filter_none.
- chevron_right.
- filter_none.
- 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!!!

No comments:
Post a Comment