input: Write a function to calculate the median age from a list of dictionaries.
  assert median_age([{'name': 'John', 'age': 42}, {'name': 'Harry', 'age': 34}, {'name': 'Jack', 'age': 29}, {'name': 'Joe', 'age': 22}, {'name': 'James', 'age': 18}]) == 29
  assert median_age([{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 40}, {'name': 'Charlie', 'age': 50}]) == 40
  assert median_age([{'name': 'Dave', 'age': 20}, {'name': 'Eve', 'age': 25}, {'name': 'Frank', 'age': 30}, {'name': 'Grace', 'age': 35}]) == 27.5
output: def median_age(data):
      ages = sorted([person['age'] for person in data])
      n = len(ages)
      if n % 2 == 1:
          return ages[n // 2]
      else:
          return (ages[(n // 2) - 1] + ages[n // 2]) / 2