Python – Ways to remove duplicates from listThis article focuses on one of the operations of getting the unique list from a list that contains a possible duplicated. Remove duplicates from list operation has large number of applications and hence, it’s knowledge is good to have. Show
Method 1 : Naive method
Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 3, 5, 6]Method 2 : Using list comprehension
Output : The original list is : [1, 3, 5, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 3, 5, 6] Method 3 : Using set()
Output : The original list is : [1, 5, 3, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 3, 5, 6]Method 4 : Using list comprehension + enumerate()
Output : The original list is : [1, 5, 3, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 5, 3, 6]Method 5 : Using collections.OrderedDict.fromkeys()
Output : The original list is : [1, 5, 3, 6, 3, 5, 6, 1] The list after removing duplicates : [1, 5, 3, 6]
Article Tags :
Python
Python list-programs python-list Practice Tags :
python-list Python | Difference of two lists including duplicatesThe ways to find difference of two lists has been discussed earlier, but sometimes, we require to remove only the specific occurrences of the elements as much they occur in other list. Let’s discuss certain ways in which this can be performed. Method #1 : Using collections.Counter()
Output :
The original list 1 : [1, 3, 4, 5, 1, 3, 3]
The original list 2 : [1, 3, 5]
The list after performing the subtraction : [1, 3, 3, 4]
Method #2 : Using map() + lambda + remove()
Output :
The original list 1 : [1, 3, 4, 5, 1, 3, 3]
The original list 2 : [1, 3, 5]
The list after performing the subtraction : [1, 3, 3, 4]
Article Tags :
Python Python Programs
Python list-programs Remove duplicates from list using SetTo remove the duplicates from a list, you can make use of the built-in function set(). The specialty of set() method is that it returns distinct elements. We have a list : [1,1,2,3,2,2,4,5,6,2,1]. The list has many duplicates which we need to remove and get back only the distinct elements. The list is given to the set() built-in function. Later the final list is displayed using the list() built-in function, as shown in the example below. The output that we get is distinct elements where all the duplicates elements are eliminated.my_list = [1,1,2,3,2,2,4,5,6,2,1] my_final_list = set(my_list) print(list(my_final_list)) Output: [1, 2, 3, 4, 5, 6]Remove Duplicates from a list using the Temporary ListTo remove duplicates from a given list, you can make use of an empty temporary list. For that first, you will have to loop through the list having duplicates and add the unique items to the temporary list. Later the temporary list is assigned to the main list. Here is a working example using temporary list. my_list = [1, 2, 3, 1, 2, 4, 5, 4 ,6, 2] print("List Before ", my_list) temp_list = [] for i in my_list: if i not in temp_list: temp_list.append(i) my_list = temp_list print("List After removing duplicates ", my_list)Output: List Before [1, 2, 3, 1, 2, 4, 5, 4, 6, 2] List After removing duplicates [1, 2, 3, 4, 5, 6]Python merge list without duplicates exampleSimple example code using set with + operator. a = ['hello', 'world'] b = ['hello', 'universe'] unique = list(set(a + b)) print(unique)Output: Another method uses NumPy You have to import a NumPy module for it. Output: [1 2 5 7 9] Combining two lists and without duplicates and don’t remove duplicates in the original listUse set to remove duplicate without removing element from the original list. list1 = [1, 2, 2, 5] list2 = [2, 5, 7, 9] res = list(set(list1 + list2)) print(res) print(list1) print(list2)Output: [1, 2, 5, 7, 9] You can do also convert the list to a new set. set_1 = set(list_1) set_2 = set(list_2)Do comment if you have any questions or suggestions on this Python list topic.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical. Share this:
RelatedPython: 5 Ways to Remove Duplicates from ListIn this article, we will learn what is a list in python. As a python list is a collection of multiple elements even containing duplicates, sometimes it is necessary to make the list unique. Here, we are going to study the multiple ways to remove duplicates from the list in python. So, let's get started! |