code: def _factor_canonicalized(lst):
      # remove duplicates from canonicalized list
  
      # we only delete out of the right end of the list,
      # once i has touched a list element, it is permantent
      lst = list(lst)
      # print 'FACTOR', lst
      # for t in lst:
      #    if not isinstance(t, (list, tuple)):
      #        t = (t,)
      #    for e in t:
      #        try:
      #            pytensor.printing.debugprint(e)
      #        except TypeError:
      #            print e, type(e)
      i = 0
      while i < len(lst) - 1:
          try:
              s_i, M_i = lst[i]
          except Exception:
              i += 1
              continue
  
          j = i + 1
          while j < len(lst):
              try:
                  s_j, M_j = lst[j]
              except Exception:
                  j += 1
                  continue
  
              if M_i is M_j:
                  s_i = s_i + s_j
                  lst[i] = (s_i, M_i)
                  del lst[j]
              else:
                  j += 1
          i += 1
      return lst
  
imports: import copy
  import logging
  import time
  import numpy
  from pytensor.tensor.rewriting.basic import register_specialize
  import pytensor.scalar
  from pytensor.compile.mode import optdb
  from pytensor.configdefaults import config
  from pytensor.graph.features import ReplacementDidNotRemoveError, ReplaceValidate
  from pytensor.graph.rewriting.basic import EquilibriumGraphRewriter, GraphRewriter, copy_stack_trace, in2out, node_rewriter
  from pytensor.graph.rewriting.db import SequenceDB
  from pytensor.graph.utils import InconsistencyError
  from pytensor.tensor import basic
  from pytensor.tensor.blas import Dot22, _dot22, _dot22scalar, batched_dot, gemm_inplace, gemm_no_inplace, gemv_inplace, gemv_no_inplace, ger, ger_destructive
  from pytensor.tensor.elemwise import DimShuffle, Elemwise
  from pytensor.tensor.exceptions import NotScalarConstantError
  from pytensor.tensor.math import Dot, _matrix_matrix_matmul, add, mul, neg, sub
  from pytensor.tensor.rewriting.elemwise import local_dimshuffle_lift
  from pytensor.tensor.type import DenseTensorType, TensorType, integer_dtypes, values_eq_approx_remove_inf_nan
label_desc: Use image node_id 7 for calling a global function with example usage: _factor_canonicalized(lst) and returns: lst