Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep duplicates in a list in Python

I know this is probably an easy answer but I can't figure it out. What is the best way in Python to keep the duplicates in a list:

x = [1,2,2,2,3,4,5,6,6,7]

The output should be:

[2,6]

I found this link: Find (and keep) duplicates of sublist in python, but I'm still relatively new to Python and I can't get it to work for a simple list.

like image 476
myname Avatar asked Apr 04 '13 13:04

myname


People also ask

Can we store duplicate values in list Python?

Python list can contain duplicate elements.

Can you have duplicates in a list?

What are duplicates in a list? If an integer or string or any items in a list are repeated more than one time, they are duplicates.

How do you keep an element from repeating in a list Python?

If the order of the elements is not critical, we can remove duplicates using the Set method and the Numpy unique() function. We can use Pandas functions, OrderedDict, reduce() function, Set + sort() method, and iterative approaches to keep the order of elements.


1 Answers

I'd use a collections.Counter:

from collections import Counter
x = [1, 2, 2, 2, 3, 4, 5, 6, 6, 7]
counts = Counter(x)
output = [value for value, count in counts.items() if count > 1]

Here's another version which keeps the order of when the item was first duplicated that only assumes that the sequence passed in contains hashable items and it will work back to when set or yeild was introduced to the language (whenever that was).

def keep_dupes(iterable):
    seen = set()
    dupes = set()
    for x in iterable:
        if x in seen and x not in dupes:
            yield x
            dupes.add(x)
        else:
            seen.add(x)

print list(keep_dupes([1,2,2,2,3,4,5,6,6,7]))
like image 121
mgilson Avatar answered Oct 16 '22 09:10

mgilson