Python "set" with duplicate/repeated elements

You are looking for a multiset.

Python's closest datatype is collections.Counter:

A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

For an actual implementation of a multiset, use the bag class from the data-structures package on pypi. Note that this is for Python 3 only. If you need Python 2, here is a recipe for a bag written for Python 2.4.


Your approach with dict with element/count seems ok to me. You probably need some more functionality. Have a look at collections.Counter.

  • O(1) test whether an element is present and current count retrieval (faster than with element in list and list.count(element))
  • counter.elements() looks like a list with all duplicates
  • easy manipulation union/difference with other Counters