Python Equality: How to Check Values Without is
In this article
In Python, == checks whether two values are equal, while is checks whether two variables point to the same object in memory. Most of the time you want value equality, not identity. This guide shows you how to use == correctly, handle edge cases like floats and custom objects, and avoid common pitfalls.
What You'll Need
- Python 3.x installed (any recent version)
- A text editor or Python REPL to test examples
Step-by-Step
- Use the
==operator for basic value comparison. Writea == bto check ifaandbhave the same value. This works for numbers, strings, lists, tuples, dictionaries, and most built-in types. For example,5 == 5returnsTrue, and[1,2] == [1,2]returnsTrue. - Compare strings by content, not by object identity. Strings are often interned in CPython, so
ismight sometimes returnTruefor equal strings, but that is an implementation detail. Always use==for string comparison:"hello" == "hello". - For numeric types,
==handles type conversion automatically.1 == 1.0returnsTruebecause both represent the same numeric value. If you need to ensure both are the same type, usetype(a) == type(b)in addition toa == b. - Compare lists and tuples element-wise.
[1,2] == [1,2]returnsTruebecause Python compares each element in order. The same applies to tuples. For sets, order does not matter:{1,2} == {2,1}isTrue. - Compare dictionaries by key-value pairs.
{'a':1} == {'a':1}returnsTrueregardless of insertion order. Dictionaries compare equal if they have the same keys and each key maps to equal values. - For floating-point numbers, consider using a tolerance. Due to binary representation,
0.1 + 0.2 == 0.3returnsFalse. Instead, useabs((0.1 + 0.2) - 0.3) < 1e-9or themath.isclose()function:import math; math.isclose(0.1+0.2, 0.3). - Compare custom objects by overriding
__eq__. If you define a class, Python uses the default__eq__which compares object identity (likeis). To make==compare values, define__eq__in your class. For example:class Point: def __init__(self, x): self.x = x def __eq__(self, other): if isinstance(other, Point): return self.x == other.x return NotImplementedThenPoint(1) == Point(1)returnsTrue. - When comparing to
None, useis(but that's an exception). The question asks about not usingis, but forNonespecifically,is Noneis the recommended idiom. If you must avoidis, you can use== None, but it may trigger__eq__on custom objects and is generally discouraged. For this guide, just note that==works forNonetoo:x == None. - Use
!=for inequality. The opposite of==is!=. It also checks value inequality, not identity.
Common Mistakes
- Using
isfor value comparison. This is the most common mistake.ischecks identity, so[] is []isFalseeven though both are empty lists. - Assuming
==always works for floats. Floating-point precision issues can cause==to returnFalsefor mathematically equal values. Always use a tolerance ormath.isclose()for floats. - Comparing different types without checking.
1 == "1"returnsFalsebecause the types differ. If you want to treat them as equal, convert explicitly. - Forgetting to override
__eq__in custom classes. Without it,==behaves likeis.
Tips
- Use
math.isclose()for float comparisons. It handles relative and absolute tolerances, making your comparisons robust. - For NumPy arrays, use
np.array_equal()ornp.allclose(). The==operator on NumPy arrays returns an element-wise boolean array, not a single boolean. - Chain comparisons for readability.
a == b == cchecks that all three are equal. - Remember that
==can be overridden. Be aware that custom classes may define__eq__to do something unexpected, so read the documentation.
Troubleshooting
If == does not return what you expect:
- Check the types. Use
type(a)andtype(b)to see if they differ. If they are different,==might returnFalseeven if values look similar. - Check for float precision. Print the full representation:
repr(0.1+0.2)shows0.30000000000000004. Usemath.isclose(). - For custom objects, verify
__eq__is defined. If not,==falls back to identity. Add__eq__or use a different comparison method. - If comparing lists or dicts, ensure the elements are comparable. If elements are custom objects, they need
__eq__as well. - If you get a
ValueErrorwith NumPy arrays, usenp.array_equal()instead of==.
Frequently Asked Questions
What is the difference between == and is?
== compares values (what the objects contain), while is compares identity (whether two variables reference the same object in memory).
Can I use == to compare two lists of custom objects?
Yes, but only if the custom objects define __eq__. Without it, list comparison will use identity for each element, so two lists with identical-looking objects will not be equal.
Why does 0.1 + 0.2 == 0.3 return False?
Because floating-point numbers are stored in binary and cannot represent some decimal fractions exactly. The sum is actually 0.30000000000000004. Use math.isclose() for such comparisons.
Does == work for comparing strings with different Unicode normalization?
No, == compares code points, so "é" (precomposed) and "é" (combining) are not equal. Use unicodedata.normalize() to normalize before comparing.
Can I compare two dictionaries with ==?
Yes, dictionaries compare equal if they have the same keys and each key maps to equal values, regardless of insertion order.
Is it ever acceptable to use is for value comparison?
Only for None and sometimes for small integers or interned strings, but that is an implementation detail. Always use == for value comparison to be safe and clear.