A simple weighted choice function - No numpy needed

I am sure you can find better, more interesting functions for a weighted choice algorithm around the net.

I reconstructed this idea from similar ideas in stackexchange.


from random import random
def weighted_choice(weights_dict):
    total_weight=sum([weights_dict[i] for i in weights_dict])
    choice=total_weight*random()
    init=0
    for i in weights_dict:
        init+=weights_dict[i]
        if init>=choice: return i

#just a little crude test
#as you can see, you can even mix and match integers and floats. In Python 3.5, the system won't mind a bit
testme={'A':3,'B':3,'C':3,'D':0.5}
outstr=''
for i in range (100):
    outstr+=str(weighted_choice(testme))
    if i%10==9:
        print (outstr)
        outstr=''