I have a program where I\'m keeping track of the success of various things using collections.Counter — each success of a thing increments the corr
collections.Counter
You can do this rather easily by using itertools.islice to get the Nth item of an iterable:
>>> import random >>> import itertools >>> import collections >>> c = collections.Counter({'a': 2, 'b': 1}) >>> i = random.randrange(sum(c.values())) >>> next(itertools.islice(c.elements(), i, None)) 'a'