Python Program to Remove Duplicate Elements from a List
ฝัง
- เผยแพร่เมื่อ 9 ก.พ. 2025
- In this tutorial you will learn to write a Python Program to Remove Duplicate Elements from a List.
The function that we create here will take a list which as duplicate elements as a parameter and it will return a list without any duplicates.
our Social Media Pages
/ exampleprogram
/ exampleprogram
/ example_program
Our Website
www.ExamplePro...
#ExampleProgram
Thanks sire u helped me a lot😘😘😘
Most welcome 😊
Thanks sire u helped me solve my assignment 🇳🇬💗💗
Most welcome 😊
Thank you. I appreciate your Support for the Community.
❤️
YOU SAVED MY LIFE!!!!!!!
Pleasure
You made your code so complex you could just convert the list to sets and all duplicates would be gone
excellent bro
To be fair: I am a beginner and I new about the x2 = list(set(x)) method but I thought I needed to figure out problem solving shit... not thinking once about the possibility of a "not in" statement... so I came here and was enlightened... only to forget it in two days haha.
Doing shit the hard way can be very helpful to learn the basics I guess.
This is exactly what I did. But you will mess up the order. So you have to sort according to the list index.
Yeah
@@moodmaker2796 this 🙌
thank you, dawg. appreciate ya
No problem
i swear i love u bro
thank you :)
thank you so much it solved my assignment
You are most welcome
Thank you very much>>>...
You are welcome!
This is really helpful
Glad this helps you :)
List1=[1,5,6,4,2,3,3,3]
Set1= set(List1)
List1=list(Set1)
print(List1)
Better than your code sir.
Bro it's user defined function 😂
Bro u put that duplicate list into set datatype and you again convert the set to the list data type and print the list like this
List1=[ 1,5,6,4,2,3,3,3]
Set1=set(list1)
List1= list(set1)
Print(list1)
Yes..
Friend i have another doubt in .py
If we want to find index of second occurrence of a number in list what we do.. ans need urgent
Please correct me if I am wrong. Why complicating things by defining function. Why not this following simple code:
data=[10, 20, 30, 30, 40, 50, 50, 60, 70]
noduplist=[]
for element in data:
if element not in noduplist:
noduplist.append(element)
print(noduplist)
#output: [10, 20, 30, 40, 50, 60, 70]
totally agree
@Prashant Rai Ok. Thank you.
good one bro!
by defining function we can , use it any no of time with any no of duplicate list , that's what functions are use for...
print(list(set(data))) try this
Use this rather maybe if you wanna reduce lines of code:
# Function
remove_duplicates = lambda input_list:list(dict({key:0 for key in input_list}))
# Example/Usage
my_list = [...]
my_list = remove_duplicates(my_list)
for loop would be performance killer if we have a big list...
list comprehension will come in handy in this case
Awsm❤️
thank you
Nice work
Thanks
Tq Sir
Welcome
Thank you
You're welcome
Can we count the duplicate numbers In a list without using count built-in function?
I want an output like...
(Number, repeated x times)
#Remove Duplicate Elements from a List and count the number of duplicate elemnets
lst=[5,2,4,-5,12,2,7,4,4]
new=[]
count=0
for i in lst:
if i not in new:
new.append(i)
#print(i)
x=len(lst)-len(new)
print("The orignal list is : ",lst)
print("The new list without duplicates is : ",new)
print("The number of repeated elements in the orignal list are : ",x)
when I heard an Indian accent I knew, I'd learn something today.
👍
We can convert the list into set to remove duplicates
thanks you are amazing 💖💖💖💖💖💖💖💖
Thanks
Welcome
It might be a REAL GOOD IDEA to post the code
thanks for the suggestion.
i will try to implement that :)
You can just convert it to a set and convert it back to a list.
This program question is asked in every interview
oh :)
I am trying to print also the noduplist by the console tells me that it is not defined. What should I do ??
For elements in duplist likhna jaruri hai kya ?
Sir pls.. say sir
Noduplist is a empty list
But how we check if the element present in noduplist or not...
See what happens is we iterate over the new list and if for example '0' is there in list , program will not execute. Therefore 'not in' is used
nice
Thanks
Please tell me how to remove alternative eliminats in a list
👍👍👍
Thank you :)
Sir ye har value ke liye kam nhi krega.
While loop se krye ek baar
data = [10,20,20,30,40,50,50]
def remove_duplicate(data, i):
if i >= len(data):
return data
if isinstance(data[i], int):
if data[i] == data[i+1]:
data.pop(i)
return remove_duplicate(data, i+1)
print(remove_duplicate(data, 0))
It would be great if you could show how to do it withouth the "not in" method. That is what I am trying to do. I am trying to loop through "unique" and if "i" is not equal to "j" then I append i to unique. However, I am doing something wrong sintaxis wise.
Can't you sort it, loop through the array comparing it to the last element and if so delete that that element/append it if not, to a noduplist. Then it sorts in nlog(n) time and runs in n time. This will run in worst case (n/2)^2. which is worse after 8 elements.
❤
thanks
It's not working in my system. Error says function object not iterable
but how to remove duplicate with that number for example numbers=[2,2,4,4,6,8,8,9] it will be unique =[6,9] . how to solve that problem?
Nice video but...?
How do you delete the '+' in the list below?
My_list=[+1222..., +1838..., 1737...]
Is this even efficient?
Sir what is the output for 1-2,2-3,3-4,4-5
This program is wrong
Your explanation is not to the point.
Can't understand what are saying.