Removing duplicate elements in a list using pop method

Question:

I have tried the below code for removing the below code for removing the duplicate items in a list but it is giving me an error as : List index out of range.

l1=[8,32,33,21,98,3,21,32,89,45,34,33,90,33,21,34,33]
print('Original List:',l1)
i=0
while i <len(l1):
    no=l1[i]
    for x in range(i+1,len(l1)):
        val=l1[x]
        if val==no:
            l1.pop(x)
    i+=1
print('List after removing duplicate items is: ',l1)

Please help me the error and how can i rectify the error

Asked By: Python Learner

||

Answers:

The reason it’s throwing an error is because your inner loop is only setting the length once. So as as soon as you start removing items, the length it is is shorter than the length it will try to iterate.
You could try changing the inner loop to a while loop. But a more pythonic way would be to use for-loops.

But considering you’re only using a simple equality comparisons on elements why don’t you try this.

l1 = list(dict.fromkeys(l1))

dict.fromkeys creates a dictionary with the keys set to the values of the first argument and then sets the values to None by default. So basically it outputs:
{8: None, 32: None, 33: None, 21: None, 98: None, 3: None, 89: None, 45: None, 34: None, 90: None}

Since a dictionary can’t have duplicate keys, the duplicates are gone.

Then calling list() on it turns it into a list by using the new dictionary’s keys as values.

If you must do it with a loop, you could try and do it constructively:

l1=[8,32,33,21,98,3,21,32,89,45,34,33,90,33,21,34,33]
print('Original List:',l1)
new_list = []
for x in l1:
    if x not in new_list:
        new_list.append(x)
print('List after removing duplicate items is: ', new_list)

But if you really have to use pop() and loops. You can do it like this:

for i, x in reversed(list(enumerate(l1))): #loop through the list from the end (with the index of that item)
    if l1.count(x) > 1: # if there is more than one of that element in the list, pop the last element you tested.
        l1.pop(i)

If order doesn’t matter, the way I would do it is by using a set:
l1 = list(set(l1))

Answered By: Lord Ratte

Below program will give nonintersection of two list with remove duplicates. With append & pop method both explained.

def list_nonintersection(list1, list2):
    nonintersection_list = []
    for x in list1:
        if x not in list2:
            nonintersection_list.append(x)
    for x in list2:
        if x not in list1:
            nonintersection_list.append(x)
    #return nonintersection_list
    # final_list =[]
    # for x in nonintersection_list:
    #     if x not in final_list:
    #         final_list.append(x)
    # return final_list
    i = 0
    while(i < len(nonintersection_list)):
        elem = nonintersection_list[i]
        new_length = len(nonintersection_list)
        for x in range(i+1, new_length-1):
            if nonintersection_list[x] == elem:
                nonintersection_list.pop(x)
        i+=1
    return nonintersection_list
Answered By: Varun Kumar