Search code examples
pythontuplesequality

Tuple comparison 'A' == ('A'), how to avoid that?


I was puzzled by seeing that python returns True for a comparison like this: 'A' == ('A')

then I found this explanation from this question.

Python compares every element in the tuple to the other term of comparison. My question is how can I avoid that? What I'm looking for is a '==' logical operator that returns True for 'A' == 'A' or ('A') == ('A') but false for 'A' ==' ('A').


Solution

  • Your "explanation" has nothing to do with your actual problem.

    ('A') is not a tuple. It is simply a string. A single-element tuple is defined like this: ('A',). When you use an actual tuple, your comparison correctly returns False:

    >>> 'A' == ('A',)
    False