You are looking for a multiset.
Python’s closest datatype is collections.Counter
:
A
Counter
is adict
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. TheCounter
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.