code: def with_fallback(preferred: Dict[str, Any], fallback: Dict[str, Any]) -> Dict[str, Any]:
      
      def merge(preferred_value: Any, fallback_value: Any) -> Any:
          if isinstance(preferred_value, dict) and isinstance(fallback_value, dict):
              return with_fallback(preferred_value, fallback_value)
          elif isinstance(preferred_value, dict) and isinstance(fallback_value, list):
              # treat preferred_value as a sparse list, where each key is an index to be overridden
              merged_list = fallback_value
              for elem_key, preferred_element in preferred_value.items():
                  try:
                      index = int(elem_key)
                      merged_list[index] = merge(preferred_element, fallback_value[index])
                  except ValueError:
                      raise ConfigurationError("could not merge dicts - the preferred dict contains "
                                               f"invalid keys (key {elem_key} is not a valid list index)")
                  except IndexError:
                      raise ConfigurationError("could not merge dicts - the preferred dict contains "
                                               f"invalid keys (key {index} is out of bounds)")
              return merged_list
          else:
              return copy.deepcopy(preferred_value)
  
      preferred_keys = set(preferred.keys())
      fallback_keys = set(fallback.keys())
      common_keys = preferred_keys & fallback_keys
  
      merged: Dict[str, Any] = {}
  
      for key in preferred_keys - fallback_keys:
          merged[key] = copy.deepcopy(preferred[key])
      for key in fallback_keys - preferred_keys:
          merged[key] = copy.deepcopy(fallback[key])
  
      for key in common_keys:
          preferred_value = preferred[key]
          fallback_value = fallback[key]
  
          merged[key] = merge(preferred_value, fallback_value)
      return merged
docs: Deep merge two dicts, preferring values from `preferred`.