# Understanding Copy in Python (With Clear Examples)

One of the most confusing concepts for beginners in Python is how copying works — especially with lists.

Let’s understand it step by step.

📌 Example 1: Simple Assignment (No Copy Happens)

myListOne = \[1, 2, 3\]

myListTwo = myListOne

Here, no new list is created.

Both myListOne and myListTwo are pointing to the same list in memory.

If we modify one:

myListOne\[0\] = 55

Now the list becomes:

\[55, 2, 3\]

And both variables will show:

\[55, 2, 3\]

Why?

Because they are referencing the same object in memory.

📌 Example 2: Creating a New List Manually

myListOne = \[1, 2, 3\]

myListTwo = \[1, 2, 3\]

Now these are two separate list objects in memory, even though they contain the same elements.

If we modify one:

myListOne\[0\] = 55

Then:

myListOne → \[55, 2, 3\]

myListTwo → \[1, 2, 3\]

Because they are different objects.

📌 Example 3: Proper Way to Copy a List

If you want:

Same elements

But different memory object

You can do:

✔ Method 1: Using Slicing

myListTwo = myListOne\[:\]

✔ Method 2: Using list()

myListTwo = list(myListOne)

✔ Method 3: Using copy module

import copy

myListTwo = copy.copy(myListOne)

All of these create a new list object with the same elements.

📌 Important Difference: == vs is

myListTwo == myListOne # True

✔ == checks if values are equal.

myListTwo is myListOne # False

✔ is checks if both variables reference the same object in memory.

📌 Pro Tip: Use id() to Check Memory

print(id(myListOne))

print(id(myListTwo))

If the IDs are the same → same object.

If different → separate objects.

⚠️ One More Important Concept: Shallow vs Deep Copy

All the copying methods shown above are shallow copies.

If your list contains nested lists:

myListOne = \[\[1, 2\], \[3, 4\]\]

myListTwo = myListOne\[:\]

The outer list is copied, but the inner lists are still shared.

To completely copy everything:

import copy

myListTwo = copy.deepcopy(myListOne)

Now both the lists are independent.

📌Final Mental Model

Variables don’t store lists.

Variables store references to objects in memory.

\= → Copies reference

\[:\] → Shallow copy

copy.copy() → Shallow copy

copy.deepcopy() → Deep copy

\== → Value comparison

is → Memory comparison

You can refer to the below video from chai and code which helped me to clear this concept 👇

https://youtu.be/brp5aiuWfso?si=lZfTk3Lb4RnazchC
