- 1

# write a function in python to remove duplicates

write a function in python to remove duplicates from a list. number = [23,45,67,78,'Hello',34.34,34j,'Python',34,45.6,'ML',78,'Hello',34.34,34j] Answer : duplicate =[23,45,67,78,'Hello',34.34,34j,'Python',34,45.6,'ML',78,'Hello',34.34,34j] print(list(set(duplicate))) O/p: ['Hello', 34.34, 67, 34, 34j, 'Python', 45, 78, 45.6, 'ML', 23] Is it right?? How do I do it without inbuilt function set ??

5th Feb 2021, 5:26 AM
rajul
+ 6
The easiest way is to loop through the list as been said by Jan markus. However, this approach is best for small lists, it's really going to wreck your code performance if you're working with a list of maybe 5000 items. What's going to be the fastest assuming a large percentage of your list are duplicate, is to create a list comprehension that would reference itself as it is being built. def foo(my_list): return [_ for _ in my_list if _ not in locals()['_[someIndex]'].__self__] You can compare the time complexity of this method, it's 30% faster.
5th Feb 2021, 7:03 AM
Mirielle
+ 3
Post your attempt first to get help from the community .
5th Feb 2021, 5:35 AM
Alphin K Sajan
+ 2
duplicate =[23,45,67,78,'Hello',34.34,34j,'Python',34,45.6,'ML',78,'Hello',34.34,34j] print(list(set(duplicate))) Op:['Hello', 34.34, 67, 34, 34j, 'Python', 45, 78, 45.6, 'ML', 23]
5th Feb 2021, 5:41 AM
rajul
0
number = [1, 2,2,3,4,5] num = set(number) number=list(num) # as we know python set doesn't allow duplicate elements in it so a simple type casting is the answer.
5th Feb 2021, 1:30 PM
Yogesh Singh