How to remove duplicates from a list without using set data type?
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 ( 7 )
res = []
for i in test_list:
if i not in res:
res.append(i)
res = []
for i in test_list:
if i not in res:
res.append(i)
l=[1,2,3,3,4,4,5]
for i in l:
while l.count(i)>1:
l.remove(i)
Create a dictionary, using the List items as keys. This will automatically remove any
duplicates because dictionaries cannot have duplicate keys.
mylist1 = [1,1,2,2,3,3,3,3,4]
mylist1 = list(dict.fromkeys(mylist1))
print(mylist1)
#output
1 2 3 4
li = list(map(int, input().split()))
emp = []
for i in range(len(li)):
if(li[i] in emp):
continue
else:
emp.append(li[i])
>> l1=[1,2,3,2,4,1,5,3,6]
#l1 is the given list, we need to remove duplicates from the list l1
#create a new empty list
l2=[]
#loop for all elements in l1
for l in l1:
if(l not in l2):
l2.append(l)
print(l2)
output-
[1,2,3,4,5,6]
t=[1,2,1,1,2,3,4,4]
p=[]
[p.append(x) for x in t is x not in p]
print(p)