self.population = population
self.cumweights = []
cumsum = 0
for w in weights:
    cumsum += w
self.cumweights.append(cumsum)

def __len__(self):
    return self.cumweights[-1]

def __getitem__(self, i):
    if not 0 <= i < len(self):
        raise IndexError(i)
return self.population[bisect.bisect(self.cumweights, i)]
Weighted random sample in python