Using the is keyword in Python

problem

You want to compare two objects that are the same. More specific, to check if they point to the same object in memory. Python has a built-in keyword for this, the is keyword.

SOLUTION

Let’s create two number lists with the same contents. Then, let’s use the is keyword to see if the first list is the same with the second.

is_keyword.py
				
					
if __name__ == '__main__':

   nums = [1, 3, 5]
   numbers = [1, 3, 5]

   print(nums is numbers)

				
			
running

I’m running it via the IDE, in this case PyCharm as shown below:

 

PyCharm running is keyword program
output
The output is False even if they have the same contents. Let’s change the code, introduce a new variable: equals and assign the first list to it. Then we will check with the is keyword if it’s the same, as follows:
is_keyword.py
				
					
if __name__ == '__main__':

   nums = [1, 3, 5]
   numbers = [1, 3, 5]

   equal = numbers

   print(equal is numbers)

				
			
output

True. The variable equal points to the same object as numbers and the Boolean expression: “equal is numbers” is returning us True.

Change the object
Let’s add an item to the numbers list and then check again for equality.
is_keyword.py
				
					
numbers.append(7)

print(equal is numbers)
				
			
output
True. The equal variable still points to the same object reference of numbers.
Removing by reference

If we remove an item from the equal list that points to the numbers list, the item will be removed from the numbers list as shown with the code below.

is_keyword.py
				
					
equal.pop() # it’s linked to numbers, so it will remove it there

print(numbers)

				
			
output

[1, 3, 5]

That’s because equal points to numbers list. That means it simply acts as an alias in this case to the numbers and it directly manipulates it.

For example,

equal.pop() will have the same result as calling numbers.pop()

The same goes for any function available to this object, in this case list.

That’s because both variables point to the same object in memory as shown in the figure below:

Two variables pointint to the same object in memory - Python is keyword

conclusion

The variables equal and numbers are like pointers. We can have more pointers if we want. If the is keyword is used between any two pointers that point to the same object then it returns True, otherwise False.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies (Ads)

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings. We use Google Ads on this site.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.