The double equals == operator and the is keyword are commonly used statements for comparing objects in Python. In this article, you’ll learn the difference between these two statements with the help of several examples.

The Double Equals == Operator

The double equals == operator returns True when the values assigned to the objects being compared are equal. The == operator doesn’t match the memory locations of two objects while comparing the object so, even if two objects have different memory locations but the same values, the double equals operator will return True. In short, the == operator compares objects for equality. Let’s see an example.

The script below defines three lists car1, car2, and car3.

car1 = ["Honda", "Toyota", "Ford"]
car2 = ["Ford", "Toyota"]
car3 = ["Honda", "Toyota", "Ford"]

Now, we’re going to use the == operator to compare lists car1 and car2. Since the two lists contain different items, the comparison will return False.

if car1 == car2:
    print("values are equal")
else:
    print("values are not equal")

Output:

values are not equal

Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.


Let’s see another example where we’ll compare the lists car1 and car3.

if car1 == car3:
    print("values are equal")
else:
    print("values are not equal")

Since the two lists in this comparison have exactly the same number and type of items, the == operator returns True.

Output:

values are equal

The is keyword

The is keyword compares identities of two or more objects by matching their memory locations. Even if two objects contain same items, the is keyword will return False if the objects do not point to the same memory location.

Let’s see an example. The following script compares the car1 and car3 lists using the is keyword. Recall that these two lists contain the same items.

if car1 is car3:
    print("values are equal")
else:
    print("values are not equal")

Output:

values are not equal

The output above shows that the comparison returns False despite the car1 and car3 lists containing the same items in the same order. So, why is it returning False? It’s because the memory locations of lists car1 and car3 are different.

You can check the memory location of an object by passing the object to the id() method. The following script prints the memory locations of our car1 and car3 lists.

print(id(car1))
print(id(car3))

The output below shows that memory locations for our list objects are indeed different.

Output:

1609071352384
1609071351936

Let’s assign the car1 list object to the car3 list object instead of hardcoding the items in the lists, like we did when we first defined them. This will make the car3 object point to the same memory location as the car1 object.

car3 = car1

Let’s again check the memory locations of our two lists.

print(id(car1))
print(id(car3))

Output:

1609071352384
1609071352384

From the above output you can see that now both the car1 and car3 lists point to the same memory location.

Now that we’ve done that, let’s compare the car1 and car3 lists one more time using the is keyword.

if car1 is car3:
    print("values are equal")
else:
    print("values are not equal")

Since the comparison returns True, you’ll now see the following output:

Output:

values are equal

Conclusion

To conclude, the double equals == operator matches equality and compares two objects for their values, irrespective of their memory locations. On the other hand, the is keyword matches identity of objects by comparing their memory locations.


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.