Python remove duplicates from list
In this post, you will learn how to remove duplicates from a list using the Python programming language.
A list is an arranged collection of elements. It is used to store collections of data. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements. Individual elements can be replaced even after the list has been created. This is one of the leading reasons why lists are being widely used.
Here, we have mentioned different ways to remove the duplicate elements from the list in Python.
Remove duplicates from list using Naive method
In the given program, we have used the naïve approach in which the program checks each position in the input string for the query pattern. In case it matches the condition, it follows the statements. By using the naive method, we have used the for loop to traverse the list and then append the first occurrence of the element in the new list, and then all other elements are ignored.
# initializing list
num_list = [21, 14, 11, 31, 21, 14, 16, 42, 11]
print ("Original list is: " + str(num_list))
# remove duplicated from list
result = []
for i in num_list:
if i not in result:
result.append(i)
# printing list after removal
print ("The list after removing duplicates : " + str(result))
Output of the above code:
Original list is: [21, 14, 11, 31, 21, 14, 16, 42, 11]
The list after removing duplicates : [21, 14, 11, 31, 16, 42]
Remove duplicates from list using a list comprehensive
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Here, we have used this approach to create a new list by performing an operation on each item in the existing list, i.e., it returned a new list after removing the duplicates from the list.
# initializing list
num_list = [21, 14, 11, 31, 21, 14, 16, 42, 11]
print ("Original list is: " + str(num_list))
# remove duplicated from list
result = []
[result.append(x) for x in num_list if x not in result]
# printing list after removal
print ("List after removing duplicates: " + str(result))
Output of the above code:
Original list is: [21, 14, 11, 31, 21, 14, 16, 42, 11]
List after removing duplicates: [21, 14, 11, 31, 16, 42]
Remove duplicates from list using set() method
The set() function creates a set object. It is used to convert any of the iterable to sequence of iterable elements with distinct elements.
# initializing list
num_list = [21, 14, 11, 31, 21, 14, 16, 42, 11]
print ("Original list is: " + str(num_list))
# to remove duplicated from list
num_list = list(set(num_list))
# printing list after removal
print ("List after removing duplicates: " + str(num_list))
Output of the above code:
Original list is: [21, 14, 11, 31, 21, 14, 16, 42, 11]
List after removing duplicates: [42, 11, 14, 16, 21, 31]
Remove duplicates from list using list comprehensive + enumerate()
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
The enumerate() is a predefined function in Python and is used to add a counter to an iteration and return it in the form of an enumerate object. In the given example, we have used both list comprehensive and enumerate methods to remove duplicate elements from the list.
# initializing list
num_list = [33, 31, 41, 33, 11, 31, 25, 12]
print ("Original list is: " + str(num_list))
# removing duplicated from list
result = [i for n, i in enumerate(num_list) if i not in num_list[:n]]
# printing list after removal
print ("List after removing duplicates: " + str(result))
Output of the above code:
Original list is: [33, 31, 41, 33, 11, 31, 25, 12]
List after removing duplicates: [33, 31, 41, 11, 25, 12]
Remove duplicates from list using *set()
The *set() method of Python is the fastest method to achieve some task. It is easy to implement. This removes the duplicates and returns a dictionary, which has to be converted to a list.
lst = [11, 42, 22, 42, 11, 56, 34]
print("Original List: ", lst)
result = [*set(lst)]
print("List after removing duplicates: ", result)
Output of the above code:
Original List: [11, 42, 22, 42, 11, 56, 34]
List after removing duplicates: [34, 42, 11, 22, 56]
Remove duplicates from list using in, not in operators
The in operator in Python checks whether a specified value is a constituent element of a sequence, like list. The not in operator in Python works exactly the opposite way as the in operator works. In the given program, we used both operators to remove duplicates from the list.
# Python 3 code to demonstrate
# removing duplicate elements from the list
# initializing list
lst = [11,67,43,11,90,67]
print ("The original list: "+ str(lst))
result = []
for i in lst:
if i not in result:
result.append(i)
# printing list after removal
print ("The list after removing duplicates : "+ str(result))
Output of the above code:
The original list: [11, 67, 43, 11, 90, 67]
The list after removing duplicates : [11, 67, 43, 90]
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary