code: def flatten_dict(
      dt: Dict,
      delimiter: str = "/",
      prevent_delimiter: bool = False,
      flatten_list: bool = False,
  ):
      """Flatten dict.
  
      Output and input are of the same dict type.
      Input dict remains the same after the operation.
      """
  
      def _raise_delimiter_exception():
          raise ValueError(
              f"Found delimiter `{delimiter}` in key when trying to flatten "
              f"array. Please avoid using the delimiter in your specification."
          )
  
      dt = copy.copy(dt)
      if prevent_delimiter and any(delimiter in key for key in dt):
          # Raise if delimiter is any of the keys
          _raise_delimiter_exception()
  
      while_check = (dict, list) if flatten_list else dict
  
      while any(isinstance(v, while_check) for v in dt.values()):
          remove = []
          add = {}
          for key, value in dt.items():
              if isinstance(value, dict):
                  for subkey, v in value.items():
                      if prevent_delimiter and delimiter in subkey:
                          # Raise if delimiter is in any of the subkeys
                          _raise_delimiter_exception()
  
                      add[delimiter.join([key, str(subkey)])] = v
                  remove.append(key)
              elif flatten_list and isinstance(value, list):
                  for i, v in enumerate(value):
                      if prevent_delimiter and delimiter in subkey:
                          # Raise if delimiter is in any of the subkeys
                          _raise_delimiter_exception()
  
                      add[delimiter.join([key, str(i)])] = v
                  remove.append(key)
  
          dt.update(add)
          for k in remove:
              del dt[k]
      return dt
  
file_path: ray/python/ray/_private/dict.py
imports: import copy
  from collections import deque
  from collections.abc import Mapping, Sequence
  from typing import Dict, List, Optional, TypeVar, Union
  from ray.util.annotations import Deprecated
label_desc: Use image node_id 3 for calling a global function with example usage: flatten_dict(dt, delimiter, prevent_delimiter, flatten_list) and returns: dt