Ways to remove duplicates from list in Python Last Updated : 18 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we'll learn several ways to remove duplicates from a list in Python. The simplest way to remove duplicates is by converting a list to a set.Using set()We can use set() to remove duplicates from the list. However, this approach does not preserve the original order. Python a = [1, 2, 2, 3, 4, 4, 5] # Remove duplicates by converting to a set a = list(set(a)) print(a) Output[1, 2, 3, 4, 5] Note: Using set() removes duplicates but does not guarantee the order of elements.Let's explore other different ways to remove duplicates from list:Table of ContentUsing For LoopUsing List ComprehensionUsing Dictionary fromkeys()Using For LoopTo remove duplicates while keeping the original order, we can use a loop (for loop) to add only unique items to a new list. Python a = [1, 2, 2, 3, 4, 4, 5] # Create an empty list to store unique values res = [] # Iterate through each value in the list 'a' for val in a: # Check if the value is not already in 'res' if val not in res: # If not present, append it to 'res' res.append(val) print(res) Output[1, 2, 3, 4, 5] Note: This method preserves the original order but may be slower for large lists due to repeated membership checks ( ‘in’ operator).Using List ComprehensionList comprehension is another way to remove duplicates while preserving order. This method provides a cleaner and one-liner approach. Python a = [1, 2, 2, 3, 4, 4, 5] # Create an empty list to store unique values res = [] # Use list comprehension to append values to 'res' # if they are not already present [res.append(val) for val in a if val not in res] print(res) Output[1, 2, 3, 4, 5] Note: This method is concise but it is less efficient for large lists similar to the loop approach.Using Dictionary fromkeys()Dictionaries maintain the order of items and by using dict.fromkeys() we can create a dictionary with unique elements and then convert it back to a list. Python a = [1, 2, 2, 3, 4, 4, 5] # Remove duplicates using dictionary fromkeys() a = list(dict.fromkeys(a)) print(a) Output[1, 2, 3, 4, 5] Note: This method is generally faster than the loop method for larger lists. Comment More infoAdvertise with us Next Article Ways to remove duplicates from list in Python M manjeet_04 Follow Improve Article Tags : Python python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python | Remove consecutive duplicates from list Removing consecutive duplicates from a list means eliminating repeated elements that appear next to each other in the list. If an element repeats consecutively, only the first occurrence should remain and the duplicates should be removed.Example:Input: ['a', 'a', 'b', 'b', 'c', 'a', 'a', 'a']Output: 3 min read Remove All Duplicates from a Given String in Python The task of removing all duplicates from a given string in Python involves retaining only the first occurrence of each character while preserving the original order. Given an input string, the goal is to eliminate repeated characters and return a new string with unique characters. For example, with 2 min read Python | Remove all duplicates words from a given sentence Goal is to process a sentence such that all duplicate words are removed, leaving only the first occurrence of each word. Final output should maintain the order of the words as they appeared in the original sentence. Let's understand how to achieve the same using different methods:Using set with join 4 min read Python - Removing duplicate dicts in list In Python, we may often work with a list that contains dictionaries, and sometimes those dictionaries can be duplicates. Removing duplicate dictionaries from a list can help us clean our data. In this article, we will explore various methods to Remove duplicate dicts in the listUsing Set and frozens 3 min read Ways to remove particular List element in Python There are times when we need to remove specific elements from a list, whether itâs filtering out unwanted data, deleting items by their value or index or cleaning up lists based on conditions. In this article, weâll explore different methods to remove elements from a list.Using List ComprehensionLis 2 min read Like