input: Write a function to sort a list of dictionaries by a key in each dictionary. The function should return the sorted list based on the specified key. If the key does not exist in any of the dictionaries, it should raise a KeyError.
  
  def sort_list_of_dicts_by_key(my_list, key):
      return sorted(my_list, key=lambda k: k[key])
  
  assert sort_list_of_dicts_by_key([{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 21}, {'name': 'Adam', 'age': 26}], 'age') == [{'name': 'John', 'age': 21}, {'name': 'Tom', 'age': 25}, {'name': 'Adam', 'age': 26}]
  assert sort_list_of_dicts_by_key([{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 21}, {'name': 'Adam', 'age': 26}], 'name') == [{'name': 'Adam', 'age': 26}, {'name': 'John', 'age': 21}, {'name': 'Tom', 'age': 25}]
  assert sort_list_of_dicts_by_key([{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 21}, {'name': 'Adam', 'age': 26}], 'age') == [{'name': 'John', 'age': 21}, {'name': 'Tom', 'age': 25}, {'name': 'Adam', 'age': 26}]
output: def sort_list_of_dicts_by_key(my_list, key):
      for d in my_list:
          if key not in d:
              raise KeyError(f"Key '{key}' not found in dictionary")
      return sorted(my_list, key=lambda k: k[key])