Amazon Interview Questions | How to remove duplicates from a list?

Question

Example

in progress 0
TheDataMonk 55 years 8 Answers 1434 views Grand Master 0

Answers ( 8 )

  1. list=set(list)

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

  3. li = list(map(int, input().split()))
    emp = []
    for i in range(len(li)):
    if(li[i] in emp):
    continue
    else:
    emp.append(li[i])

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

  5. t=[1,2,1,1,2,3,4,4]
    p=[]
    [p.append(x) for x in t is x not in p]
    print(x)

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

  7. We can simply remove duplicate from a list by converting in into set data type and then again into list type.

Leave an answer

Browse
Browse