How to remove duplicates from a list without using set data type?

Question

Code

in progress 0
TheDataMonk 55 years 7 Answers 1401 views Grand Master 0

Answers ( 7 )

    0

    res = []
    for i in test_list:
    if i not in res:
    res.append(i)

  1. res = []
    for i in test_list:
    if i not in res:
    res.append(i)

  2. l=[1,2,3,3,4,4,5]
    for i in l:
    while l.count(i)>1:
    l.remove(i)

  3. 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

  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])

  5. >> 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]

  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)

Leave an answer

Browse
Browse