def gini_index(groups: list, class_values: list) -> float:
    """
    Calculate the Gini index for a split dataset (0 is perfect split)
    :param groups: to compute the gini index (last item is class value)
    :param class_values: class values present in the groups
    :return: gini index
    """
    gini = 0.0
    for class_value in class_values:
        for group in groups:
            size = len(group)
            if size == 0:
                continue
            proportion = [row[-1] for row in group].count(class_value) / float(size)
            gini += (proportion * (1.0 - proportion))
    return gini