GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Shared Reference in Python - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Shared Reference in Python

Last Updated : 27 Jul, 2026

A shared reference occurs when multiple variables refer to the same object in memory. Instead of creating a new object during assignment, Python creates another reference to the existing object. Shared references are also needed because changes made to mutable objects, such as lists and dictionaries, can affect all variables that reference the same object.

Example:

Python
x = 5
y = x
print(x)
print(y)

Output
5
5

Reassigning Variables

Reassigning a variable creates a new reference without affecting other variables.

Python
x = 5
y = x
x = "Geeks"
print(x)
print(y)

Output
Geeks
5

Explanation:

  • Initially, both x and y reference 5.
  • x = "Geeks" creates a new string object.
  • x now references "Geeks".
  • y still references 5.

If an object is no longer referenced, Python eventually reclaims its memory through garbage collection.

Shared References and Mutable Objects

Mutable objects can be modified after creation. This requires extra care when using shared references.

Python
L1 = [1, 2, 3, 4, 5]
L2 = L1
L1[0] = 0
print(L1)
print(L2)

Output
[0, 2, 3, 4, 5]
[0, 2, 3, 4, 5]

Explanation:

  • L2 = L1 creates a shared reference.
  • Both variables point to the same list.
  • L1[0] = 0 modifies the original list in place.
  • The changes are visible through both variables.

Creating Independent Copies

If separate objects are needed, create a copy instead of a shared reference.

Python
L1 = [1, 2, 3, 4, 5]
L2 = L1[:]
L1[0] = 0
print(L1)
print(L2)

Output
[0, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

Explanation:

  • L1[:] creates a shallow copy of the list.
  • L1 and L2 become independent objects.
  • Modifying L1 no longer affects L2.

Note: List slicing only works for lists. Use .copy() for dictionaries and sets.

Comparing Objects Using == and is

Python provides two ways to compare objects.

Example 1: Shared Reference

Python
L1 = [1, 2, 3]
L2 = L1
print(L1 == L2)
print(L1 is L2)

Output
True
True

Explanation:

  • == compares object values.
  • is compares object identities.
  • Since both variables reference the same object, both return True.

Example 2: Different Objects With Same Values

Python
L1 = [1, 2, 3]
L2 = [1, 2, 3]
print(L1 == L2)
print(L1 is L2)

Output
True
False

Explanation:

  • Both lists contain identical values.
  • They are stored as separate objects in memory.
  • == returns True.
  • is returns False.

Small Integer and String Caching

Python internally caches some frequently used immutable objects such as small integers and certain strings to improve performance.

Python
a = 50
b = 50
print(a == b)
print(a is b)

Output
True
True

Explanation:

  • Python may reuse the same object for small integers.
  • a and b can reference the same cached object.
  • Therefore, is may also return True.

Note: Object caching is an implementation detail and should not be relied upon in programs. Use == for value comparisons and reserve is for identity checks (for example, value is None).

Assignment vs Copying in Python

OperationCreates New Object?Shared Reference?
b = aNoYes
b = a[:]Yes (lists only)No
b = a.copy()YesNo
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.