# Buggy code
def _contains(self, other):
    from sympy.matrices import Matrix
    from sympy.solvers.solveset import solveset, linsolve
    from sympy.utilities.iterables import iterable, cartes

    L = self.lamda
    if self._is_multivariate():
        if not iterable(L.expr):
            if iterable(other):
                return S.false
            return other.as_numer_denom() in self.func(
                Lambda(L.variables, L.expr.as_numer_denom()), self.base_set
            )
        if len(L.expr) != len(self.lamda.variables):
            raise NotImplementedError(
                filldedent("""
    Dimensions of input and output of Lambda are different.""")
            )
        eqs = [expr - val for val, expr in zip(other, L.expr)]
        variables = L.variables
        free = set(variables)
        if all(i.is_number for i in list(Matrix(eqs).jacobian(variables))):
            solns = list(
                linsolve([e - val for e, val in zip(L.expr, other)], variables)
            )
        else:
            syms = [e.free_symbols & free for e in eqs]
            solns = {}
            for i, (e, s, v) in enumerate(zip(eqs, syms, other)):
                if not s:
                    if e != v:
                        return S.false
                    solns[vars[i]] = [v]
                    continue
                elif len(s) == 1:
                    sy = s.pop()
                    sol = solveset(e, sy)
                    if sol is S.EmptySet:
                        return S.false
                    elif isinstance(sol, FiniteSet):
                        solns[sy] = list(sol)
                    else:
                        raise NotImplementedError
                else:
                    raise NotImplementedError
            solns = cartes(*[solns[s] for s in variables])
    else:
        # assume scalar -> scalar mapping
        solnsSet = solveset(L.expr - other, L.variables[0])
        if solnsSet.is_FiniteSet:
            solns = list(solnsSet)
        else:
            raise NotImplementedError(
                filldedent(
                    """
                Determining whether an ImageSet contains %s has not
                been implemented."""
                    % func_name(other)
                )
            )
    for soln in solns:
        try:
            if soln in self.base_set:
                return S.true
        except TypeError:
            return self.base_set.contains(soln.evalf())
    return S.false


# Traceback (TypeError)
Following function call generate an error in Python3 but work well in Python2.

factor_list(x*(x+y))

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/thilinarmtb/GSoC/sympy/sympy/polys/polytools.py", line 5675, in factor_list
return _generic_factor_list(f, gens, args, method='factor')
File "/home/thilinarmtb/GSoC/sympy/sympy/polys/polytools.py", line 5423, in _generic_factor_list
fp = _sorted_factors(fp, method)
File "/home/thilinarmtb/GSoC/sympy/sympy/polys/polytools.py", line 5322, in _sorted_factors
return sorted(factors, key=key)
TypeError: unorderable types: list() < int()


Similarly following calls generate the same error:
[1] factor_list(y*(x+y)),  factor_list(x*y + y**2)
[2] factor_list(x*(x+y+z)), factor_list(x**2+x*y+x*z)
[3] factor_list((x + 1)*(x + y))

But the following don't:
[4] factor_list(x*(x**2 + y**2)), factor_list(x**3 + x*y**2)
[5] factor_list(x**3 - x),  factor_list(x*(x**2 - 1))
[6] factor_list(x**2 + x*y + x + y)
[7] factor_list((x + z)*(x + y)), factor_list(x**2 + x*z + y*x + y*z)

Here [6] is mathematically same as [3]. But only [3] gives the error.

# Fixed code
def _contains(self, other):
    from sympy.matrices import Matrix
    from sympy.solvers.solveset import solveset, linsolve
    from sympy.utilities.iterables import is_sequence, iterable, cartes

    L = self.lamda
    if is_sequence(other):
        if not is_sequence(L.expr):
            return S.false
        if len(L.expr) != len(other):
            raise ValueError(
                filldedent("""
    Dimensions of other and output of Lambda are different.""")
            )
    elif iterable(other):
        raise ValueError(
            filldedent("""
    `other` should be an ordered object like a Tuple.""")
        )

    solns = None
    if self._is_multivariate():
        if not is_sequence(L.expr):
            # exprs -> (numer, denom) and check again
            # XXX this is a bad idea -- make the user
            # remap self to desired form
            return other.as_numer_denom() in self.func(
                Lambda(L.variables, L.expr.as_numer_denom()), self.base_set
            )
        eqs = [expr - val for val, expr in zip(other, L.expr)]
        variables = L.variables
        free = set(variables)
        if all(i.is_number for i in list(Matrix(eqs).jacobian(variables))):
            solns = list(
                linsolve([e - val for e, val in zip(L.expr, other)], variables)
            )
        else:
            syms = [e.free_symbols & free for e in eqs]
            solns = {}
            for i, (e, s, v) in enumerate(zip(eqs, syms, other)):
                if not s:
                    if e != v:
                        return S.false
                    solns[vars[i]] = [v]
                    continue
                elif len(s) == 1:
                    sy = s.pop()
                    sol = solveset(e, sy)
                    if sol is S.EmptySet:
                        return S.false
                    elif isinstance(sol, FiniteSet):
                        solns[sy] = list(sol)
                    else:
                        raise NotImplementedError
                else:
                    raise NotImplementedError
            solns = cartes(*[solns[s] for s in variables])
    else:
        x = L.variables[0]
        if isinstance(L.expr, Expr):
            # scalar -> scalar mapping
            solnsSet = solveset(L.expr - other, x)
            if solnsSet.is_FiniteSet:
                solns = list(solnsSet)
            else:
                msgset = solnsSet
        else:
            # scalar -> vector
            for e, o in zip(L.expr, other):
                solns = solveset(e - o, x)
                if solns is S.EmptySet:
                    return S.false
                for soln in solns:
                    try:
                        if soln in self.base_set:
                            break  # check next pair
                    except TypeError:
                        if self.base_set.contains(soln.evalf()):
                            break
                else:
                    return S.false  # never broke so there was no True
            return S.true

    if solns is None:
        raise NotImplementedError(
            filldedent(
                """
            Determining whether %s contains %s has not
            been implemented."""
                % (msgset, other)
            )
        )
    for soln in solns:
        try:
            if soln in self.base_set:
                return S.true
        except TypeError:
            return self.base_set.contains(soln.evalf())
    return S.false