code: def from_engine(
      keys: KeysCollection, first: bool = False
  ) -> Callable:
      """
      Utility function to simplify the `batch_transform` or `output_transform` args of ignite components
      when handling dictionary or list of dictionaries(for example: `engine.state.batch` or `engine.state.output`).
      Users only need to set the expected keys, then it will return a callable function to extract data from
      dictionary and construct a tuple respectively.
  
      If data is a list of dictionaries after decollating, extract expected keys and construct lists respectively,
      for example, if data is `[{"A": 1, "B": 2}, {"A": 3, "B": 4}]`, from_engine(["A", "B"]): `([1, 3], [2, 4])`.
  
      It can help avoid a complicated `lambda` function and make the arg of metrics more straight-forward.
      For example, set the first key as the prediction and the second key as label to get the expected data
      from `engine.state.output` for a metric::
  
          from monai.handlers import MeanDice, from_engine
  
          metric = MeanDice(
              include_background=False,
              output_transform=from_engine(["pred", "label"])
          )
  
      Args:
          keys: specified keys to extract data from dictionary or decollated list of dictionaries.
          first: whether only extract specified keys from the first item if input data is a list of dictionaries,
              it's used to extract the scalar data which doesn't have batch dim and was replicated into every
              dictionary when decollating, like `loss`, etc.
  
  
      """
      _keys = ensure_tuple(keys)
  
      def _wrapper(data):
          if isinstance(data, dict):
              return tuple(data[k] for k in _keys)
          if isinstance(data, list) and isinstance(data[0], dict):
              # if data is a list of dictionaries, extract expected keys and construct lists,
              # if `first=True`, only extract keys from the first item of the list
              ret = [
                  data[0][k] if first else [i[k] for i in data]
                  for k in _keys
              ]
              return tuple(ret) if len(ret) > 1 else ret[0]
  
      return _wrapper
  
file_path: MONAI/monai/handlers/utils.py
imports: from __future__ import annotations
  import os
  from collections import OrderedDict
  from collections.abc import Callable, Sequence
  from typing import TYPE_CHECKING, Any
  import numpy
  import torch
  from monai.config import IgniteInfo, KeysCollection, PathLike
  from monai.utils import ensure_tuple, look_up_option, min_version, optional_import
label_desc: Use image node_id 4 for calling a global function with example usage: from_engine(keys, first) and returns: _wrapper, tuple, unknown