0

Im trying to create a new list of unique values and remove said values from the original list so that what's left is duplicates. It appears my for loop is skipping over values.

array = [1,3,4,2,2,3,4]
def duplicates(array):
    mylist = []
    for item in array:
        if item not in mylist:
            mylist.append(item)
            array.remove(item)
    return mylist

results:

duplicates(array)

[1, 4, 2]

3
  • 2
    do you need the duplicated values or unique values? its kind of confusing Commented Oct 2, 2019 at 22:53
  • I would like to end up with a list of unique values and have the original list contain the remaining duplicate values. Commented Oct 2, 2019 at 22:55
  • 1
    can you edit your question to include the results that you want to achieve Commented Oct 2, 2019 at 22:56

6 Answers 6

1

I think that using collections.Counter is more appropriate for this task:

array = [1, 3, 4, 2, 2, 3, 4]

from collections import Counter

def duplicates(array):
  return [n for n, c in Counter(array).items() if c > 1]

print(duplicates(array))

Output:

[3, 4, 2]
Sign up to request clarification or add additional context in comments.

Comments

1

The issue is with the array.remove(item), it is deleting the element at the index position visited. So, index number reduces by one and making the loop to skip reading the next value.

[1, 3, 4, 2, 2, 3, 4] -> before 1st iteration index 0 -> value =1

[3, 4, 2, 2, 3, 4] -> After 1st iteration 1 is removed, so index 0 -> value =3(loop not reading it as it already read index 0, so loop is reading index 1 -> value 4)

Correct code to display values without duplicates:

array = [1,3,4,2,2,3,4]

def duplicates(array):
    mylist = []
    for item in array:
        if item not in mylist:
            mylist.append(item)
            #array.remove(item)
    return mylist

res=duplicates(array)
print (res)

Comments

0

You are removing values from the list you are iterating through, so your loop is skipping values, try this

array = [1,3,4,2,2,3,4]
def duplicates(array):
    mylist = []
    for i, item in enumerate(array):
        if item not in mylist:
            mylist.append(item)
            array[i] = None

    array[:] = list(filter(
        lambda x: x is not None,
        array
    ))

    return mylist

Though you should clarify what you want to do with array variable as it is currently unclear.

Comments

0
array = [1,3,4,2,2,3,4]
def duplicates(array):
    mylist = []
    for item in array:
        if item not in mylist:
            mylist.append(item)
            array.remove(item)
        else:
            array.remove(item)
    return mylist

just remove the item that you don't append

Comments

0

You do not need to use a loop, it is much clearer to use a list comprehension

dups = list(set([l for l in array if array.count(l) > 1]))

However, the answer provided by kuco 23 does this appropriately with a loop.

Comments

0

A bit unclear what result you expect. If you want to get all unique values while maintaining order of occurrence, the canonical way to achieve this would be to use a collections.OrderedDict:

from collections import OrderedDict

def duplicates(array):
    return list(OrderedDict.fromkeys(array))

>>> duplicates(array)
[1, 3, 4, 2]

If you want get a list of only duplicates, i.e. values that occur more than once, you could use a collections.Counter:

from collections import Counter

def duplicates(array):
    return [k for k, v in Counter(array).items() if v > 1]

>>> duplicates(array)
[3, 4, 2]

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.