input: Write a function to calculate the Gini coefficient of a given dataset. The function should take a list of numeric values and return the Gini coefficient as a float. Use the formula for Gini coefficient which involves calculating the frequency of each item, cumulative sum, and Lorenz values. If the dataset is empty, return 0.
  assert gini([2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 21]) == 0.605
  assert gini([1, 1, 2, 2, 3, 3]) == 0.2777777777777778
  assert gini([4, 4, 4, 4, 4, 4, 4, 4]) == 0
  assert gini([]) == 0
output: def gini(data):
      if not data:
          return 0
      n = len(data)
      data.sort()
      total_sum = sum(data)
      lorenz_values = [sum(data[:i+1]) / total_sum for i in range(n)]
      gini_coef = 1 - (2 * sum(lorenz_values) / n - 1) / n
      return gini_coef