code: def from_engine_idx(keys: KeysCollection, idx, first: bool = False):
      keys = ensure_tuple(keys)
  
      def _match(t, idx):
          p, label = t
          p_n = [x[idx, ...][None] for x in p]
          l_n = [x[idx, ...][None] for x in label]
          return p_n, l_n
  
      def _wrapper(data):
          if isinstance(data, dict):
              return _match(tuple(data[k] for k in keys), idx)
  
          if isinstance(data, list) and isinstance(data[0], dict):
              ret = [
                  data[0][k] if first else [i[k] for i in data]
                  for k in keys
              ]
              return _match(tuple(ret) if len(ret) > 1 else ret[0], idx)
  
      return _wrapper
  
imports: from monai.config import KeysCollection
  from monai.handlers import MeanDice, RootMeanSquaredError, from_engine
  from monai.utils import ensure_tuple
label_desc: Use image node_id 2 for calling a global function with example usage: from_engine_idx(keys, idx, first) and returns: _wrapper, p_n,  l_n, _match, _match