1

I'm just starting out in Python and I got across this question. Examines an array of integers and eliminates all duplication of values. The distinct integers are all moved to the left part of the array

I'm trying to find all the duplicate entries in the array, delete them, and move the rest of the items towards left.

This is the code I've written, and I just can't figure out what's wrong and how its getting out of index, I tried writing it in C++ and it works fine. Can anybody please help me and explain to me how the its getting out of index.

array = [2,2,3,5,2,5,6,2,4,8,3,36,8,2]

for i in range(len(array)-1,-1,-1):
    for j in range(0,i):
        if(array[j]==array[i]):
            del array[i]
            
print(array)

The expected output is

[2, 3, 5, 6, 4, 8, 36]
2
  • What is the expected output? [2, 3, 5, 6, 4, 8, 36]? Commented Aug 2, 2020 at 19:33
  • Yes, that is the expected output. Commented Aug 2, 2020 at 19:36

3 Answers 3

3

Once you delete the element at the current index after finding another element with the same value, you need to break out of the loop, as i no longer refers to the same element. Demo

array = [2,2,3,5,2,5,6,2,4,8,3,36,8,2]
 
for i in range(len(array)-1,-1,-1):
    for j in range(0,i):
        if(array[j]==array[i]):
            del array[i]
            break
print(array)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, That worked perfectly. I don't know how I missed that :-)
@SarthakDhonchak No problem.
This condition may be a trouble of you have three or more duplicates in a row. I believe it's better to check the size of the list.
@AndrejZacharevicz It works fine even with three or more duplicates in a row: ideone.com/afILK5
1

This happened because you were iterating over array (or list) up to the last index and change it's length at the same time. The range you created at start of the loop knows nothing about list size changes. If you choose to control current index manually, you'll have a way to avoid it. But this is a bit not pythonic, as looks overengineered.

You can just do the same task other way to avoid this issue and use the power of Python. For example, you can use set to filter all duplicates:

array = [2,2,3,5,2,5,6,2,4,8,3,36,8,2]

results = set(array)
print(result)

But, as I was pointed in comments below, this will most probably change the order of elements in list.bIf you need to remove duplicates, but leave the order intact this is definitely not the best way.

Or you can use another list to store results:

array = [2,2,3,5,2,5,6,2,4,8,3,36,8,2]
results = []

for number in array:
    if number not in results:
        results.append(number)

print(results)

3 Comments

You mean if number not in results?
Also, the second example is good if preserving order is necessary, except that it's slower. I would add a seen set as well and check if number not in seen.
@BrianMcCutchon Yes I forget the order issue in set. Thank you for pointing this out. And you proposed a great way to use set and save the order of list!
0

In the first iteration i points to the last element and j to the first element. Because they are the same you delete one element of the array. Now the array has one less element than before and the element pointed by i doesn't exist anymore. Thus, you get the index error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.