input: 
  def gini(x):
      """Calculate Gini Coefficient of a given dataset.
      >>> gini([2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,21])
      0.605
      """
      x_value_counts = x.value_counts().sort_index() 
      x_cum_sum = x_value_counts.cumsum()
      n = len(x)
      lorenz = x_cum_sum / n
      lorenz = np.insert(lorenz, 0, 0)
      width = lorenz[:-1] - lorenz[1:]
      gini_coef = (1 / n) * sum(width * (lorenz[1:] + lorenz[:-1]))
      return gini_coef
  
output:     import numpy as np
      x = np.array(x)
      x_value_counts = np.bincount(x)
      x_cum_sum = np.cumsum(x_value_counts)
      n = len(x)
      lorenz = x_cum_sum / n
      lorenz = np.insert(lorenz, 0, 0)
      width = lorenz[:-1] - lorenz[1:]
      gini_coef = (1 / n) * sum(width * (lorenz[1:] + lorenz[:-1]))
      return round(gini_coef, 3)