Creating a function to compute Gini Index I'm trying to compute the Gini Index for different examples given in this page. I don't get what I'm doing wrong, as the formula showed is:
$Gini Index = 1 - \sum_{i=1}^{C}(p_{i})^{2}$
And my code trying to implement that formula:
def gini_index(lista):
    suma = 0
    for i in set(lista):
        p = (lista.count(i) / len(lista) ) ** 2
        suma += p
    print('GI: 1 -', suma, '=', 1-suma)
  

gini_index(iris['species'].tolist())                                     # It should return 0.6667 -> CORRECT
gini_index(iris.loc[df['petal length (cm)'] < 2.45, 'species'].tolist()) # It should return 0.3333 -> WRONG
gini_index(iris.loc[df['petal length (cm)'] < 5, 'species'].tolist())    # It should return 0.4086 -> WRONG
gini_index(iris.loc[df['sepal length (cm)'] < 6.4, 'species'].tolist())  # It should return 0.5578 -> WRONG

I just can't get what's wrong with the implementation of the formula. I don't think it is a code-problem as I triple-checked it, so I think it's a statistic problem.

The page where you got the examples isn't doing the same calculation as you are.  When there's no split variable (your first example), it computes the Gini index and you compute the Gini index, and they are the same, and we're all happy.
In the remaining three examples it looks as though you're computing the Gini index for Species on a subset of the data, eg the same with petal length less than 2.45cm. The other webpage is computing a weighted sum of the Gini index on the subset with petal length less than 2.45cm. and the subset with petal length at least 2.45cm. These aren't the same.
