Python id() Function Explained

The Python id() function returns a number that identifies an object while that object exists.

Beginners usually see id() when learning how variables and objects work in Python. It can help you understand whether two variables point to the same object, especially with mutable objects like lists and dictionaries.

Use id() for inspection and debugging. Do not use it to compare normal values. For value comparison, use ==.

name = "Python"
print(id(name))

other = name
print(id(other))

In this example, other = name makes both variables refer to the same object, so the two id() values match.

Use id() to inspect object identity. Do not use it to check whether two values are equal. Use == for value comparison.

What id() does

id(obj) returns an integer that identifies an object during its lifetime.

Key points:

  • The returned value is unique for that object while it exists
  • It shows object identity, not object value
  • Beginners mostly use it for learning and debugging
  • It is helpful when you want to see whether two names refer to the same object

This is closely related to how Python variables work. A variable does not store a value in the same way a box stores an item. Instead, it refers to an object.

Basic syntax

The syntax is simple:

id(object)

Important details:

  • It takes one argument
  • It returns an integer
  • It works with strings, numbers, lists, dictionaries, functions, and other objects

Example:

text = "hello"
number = 42
items = [1, 2, 3]

print(id(text))
print(id(number))
print(id(items))

Expected output will be three integers. The exact numbers will be different on your computer.

Object identity vs value

This is the most important idea behind id().

  • == checks whether two objects have the same value
  • is checks whether two variables refer to the same object
  • id() lets you inspect that identity as an integer

Example:

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)   # True
print(a is b)   # False
print(id(a))
print(id(b))

Here:

  • a == b is True because both lists contain the same values
  • a is b is False because they are different list objects
  • id(a) and id(b) are usually different

If this topic feels confusing, it helps to read about mutability in Python and Python data types.

Simple example with shared references

When you assign one variable to another, both names can refer to the same object.

numbers = [10, 20, 30]
other_numbers = numbers

print(id(numbers))
print(id(other_numbers))
print(numbers is other_numbers)

Expected output:

True

The two id() values will also match.

This happens because other_numbers = numbers does not create a new list. It creates a second reference to the same list.

Example with separate objects

Now compare that with two separate lists that contain the same values.

list_a = [1, 2, 3]
list_b = [1, 2, 3]

print(list_a == list_b)
print(list_a is list_b)
print(id(list_a))
print(id(list_b))

Expected output:

True
False

What this means:

  • The lists are equal in value
  • They are not the same object
  • Their id() values are usually different

This is why id() should not replace ==.

When id() is useful

id() can be useful in a few situations:

  • Checking whether two variables refer to the same object
  • Understanding mutable objects like lists and dictionaries
  • Debugging code that changes data in place
  • Learning how assignment works in Python

For example, if a list changes unexpectedly, id() can help you see whether two variables point to the same list:

original = ["a", "b"]
copy_ref = original

copy_ref.append("c")

print(original)
print(copy_ref)
print(id(original))
print(id(copy_ref))

Expected output:

['a', 'b', 'c']
['a', 'b', 'c']

Both variables show the same updated list because they refer to the same object.

When not to use id()

Do not use id() as a normal comparison tool.

Avoid these mistakes:

  • Do not use it instead of == for strings, numbers, lists, or other normal values
  • Do not store or depend on id() values across program runs
  • Do not assume the integer has a meaning you should interpret
  • Do not build unnecessary program logic around id()

Bad example:

a = "cat"
b = "cat"

if id(a) == id(b):
    print("The values are equal")

This is the wrong test. Use:

if a == b:
    print("The values are equal")

Use is or id() only when identity actually matters.

Important beginner notes

There are a few details that can confuse beginners:

  • Some small values may appear to share identities because Python can reuse objects
  • That behavior can vary between Python implementations
  • Do not assume equal immutable values always have the same id()
  • Do not assume they always have different id()

The important rule is:

  • Use == to check value
  • Use is to check identity

In everyday Python code, id() is mainly an inspection tool. It is not something you use all the time.

Common mistakes

Here are common beginner mistakes with id():

  • Using id() when == should be used
  • Confusing identity with equality
  • Thinking the returned integer is a memory address you should rely on
  • Assuming equal immutable values always have different or always have the same id()
  • Using id() to make program logic decisions unnecessarily

If you are debugging, these commands are often helpful:

print(id(my_object))
print(a == b)
print(a is b)
print(type(my_object))
help(id)

FAQ

What does Python id() return?

It returns an integer that identifies an object for as long as that object exists.

Is id() the same as a memory address?

In CPython, it is often related to the memory address, but beginners should treat it as an identity value, not as something to depend on.

Should I use id() to compare two values?

No. Use == to compare values. Use id() or is only when you need to know whether two variables refer to the same object.

Why do two variables sometimes have the same id()?

Because they can point to the same object.

Why do equal lists have different id() values?

Because they can store the same values but still be separate objects.

See also