dylanjobson
dylanjobson

Reputation: 9

Python: Duplicates in list

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]

Upvotes: 0

Views: 610

Answers (6)

user2390182
user2390182

Reputation: 73450

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]

Upvotes: 0

Nori Vinay
Nori Vinay

Reputation: 11

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)

Upvotes: 1

Djaouad
Djaouad

Reputation: 22776

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]

Upvotes: 1

happysunshinekid
happysunshinekid

Reputation: 60

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.

Upvotes: 0

Pedro Dalcolli
Pedro Dalcolli

Reputation: 26

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

Upvotes: 0

kuco 23
kuco 23

Reputation: 830

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.

Upvotes: 0

Related Questions