import math


def variation_distance(data1, data2):
    """ Returns the total variation distance between the distributions
        represented by data1 and data2
    """
    union_keys = list(set(data1.keys()) | set(data2.keys()))
    distance = 0
    for key in union_keys:
        if key in data1:
            val1 = data1[key]
        else:
            val1 = 0
        if key in data2:
            val2 = data2[key]
        else:
            val2 = 0
        distance += math.fabs(val1 - val2)
    return .5 * distance