Amazon Interview Questions | How to remove duplicates from a list?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 8 )
list=set(list)
One of the ways is to use set() function. Sets are unordered
collection of distinct objects.
Example:
t = [1,1,2,2,3,4,5,5]
print(t)
#output
1 1 2 2 3 4 5 5
t_set = list(set(t))
print(t_set)
#output
1 2 3 4 5
li = list(map(int, input().split()))
emp = []
for i in range(len(li)):
if(li[i] in emp):
continue
else:
emp.append(li[i])
Temporarily convert the duplicate list to a set and then again convert it into a list. This will remove the duplicate items in the list.
t=[1,2,1,1,2,3,4,4]
p=[]
[p.append(x) for x in t is x not in p]
print(x)
print(p)
We can use the SET function to remove duplicates from the List. You can convert the list to set and again set to list.
L =[a,a,a,b,b,c,c,c,c,d,d]
New_list = LIST(SET(L))
output =a,b,c,d
We can simply remove duplicate from a list by converting in into set data type and then again into list type.