import random


def weighted_choice(choices):
    """Return a weighted random element from a non-empty sequence.

    choices is a sequence of two-element sequences. The first element of
    each sequence is the element and the second one is the weight.
    """
    total = sum(weight for element, weight in choices)
    r = random.uniform(0, total)
    upto = 0
    for element, weight in choices:
        upto += weight
        if upto >= r:
            return element
    assert False, 'Should never get here'