In most of the projects, you need to write a code to check if the list has any duplicates and if it has copies, then you need to remove them and returns the new list with the unique items. So here is a Python Remove Duplicates From List tutorial for you. In this tutorial, you will learn to remove duplicate items from your list.
But before proceeding, if you are not aware about list then check it first to learn all about list in python.
An Introduction To Python List
Contents
Python Remove Duplicates From List : 4 Ways Of Removing Duplicates From List
Python provides various ways to remove duplicates from list. You can use set() function or write the user-defined function that can remove the duplicate items and gives the unique items list. Let’s see them one by one.
By Using Set
- This is the most popular way of removing duplicates from list.
- A set is an unordered collection of data type that is mutable.
- set() automatically removes duplicates.
1 2 3 4 5 |
list1 = [9,9,5,6,3,4,1,9,2,4,2] list2 = list(set(list1)) print("New List : ", list2) |
Now let’s check the output.
Now you can see in the output duplicate items has been removed from the list.
Using Not In On The List
In this way, we first create an empty list then start a loop that goes through the list, append the first occurrence of the element in new list and ignore all the other occurrences of that particular element. Let’s see it with an example.
1 2 3 4 5 6 7 8 9 10 11 12 |
list1 = [9,9,5,6,3,4,1,9,2,4,2] print("Original List : ", list1) list2 = [] for i in list1: if i not in list2: list2.append(i) print("\n New List : ", list2) |
- The for loop looks at all the elements.
- The if statement looks for duplicates.
- append() only adds elements if they are not duplicates.
Now let’s check its output.
Using A User-Defined Function
In this way, you have to create a function where you can pass your list as an argument, and get back the list without duplicates. So write the following code and run it.
1 2 3 4 5 6 7 8 9 10 |
def remove_dups_list(items): return list(set(items)) l1=[1,1,1,4,4,4,6,5,4] print("Original List : ", l1) l2 = remove_dups_list(l1) print("New List", l2) |
- we created a function and named it remove_dups_list().
- This function will take items argument.
- Then we return those items as a set to remove duplicates. Wrapped this set in a list to return the original list in the form of a list with the duplicates removed.
So let’s go ahead and run this code.
Using Lambda Function
This is very similar to above example but here instead of using def function, we will use lambda function. So write the following code and run it.
1 2 3 4 5 6 7 8 |
remove_dups_lambda= lambda numbers : list(set(numbers)) l1=[1,1,1,4,4,4,6,5,4] print("Original List : ", l1) l2 =remove_dups_lambda(l1) print("New List", l2) |
- The first thing we have done is created a variable.
- Then next created a lambda function that takes numbers as arguments and return those numbers argument as set which will automatically removes the duplicates.
- Now Wrapped this set in a list to return the original list in the form of a list with the duplicates removed.
Now run it and see the result.
So guys that’s all about Python Remove Duplicates From List tutorial. I hope you found it helpful, if yes then please share it as much as possible. And if you have any query regarding this post then feel free to ask in comment section. HAPPY CODING.
Related Articles :
- Reverse A List Python – Reverse A List With Examples
- Join Two Lists Python – Learn Joining Two Lists With Examples
- Python Get Files In Directory Tutorial