TGViewer
Learn Python Coding Learn Python Coding @pythonre Β· 40.7K subscribers
Post #9535 1.46K
Deep copying of objects with the copy module πŸπŸ“¦

import copy

# Original list with nested structure
original = [[1, 2, 3], [4, 5, 6]]

# 1. Shallow copy
shallow = copy.copy(original)
shallow[0][0] = 'X'
# Oh no! Both lists have changed, because the nested list wasn't copied, but passed by reference
print(f"Original after shallow: {original}") # [['X', 2, 3], [4, 5, 6]]

# Restore the data
original = [[1, 2, 3], [4, 5, 6]]

# 2. Deep copy
deep = copy.deepcopy(original)
deep[0][0] = 'X'
# Everything is fine! Only deep has changed, the original remains untouched
print(f"Original after deep: {original}") # [[1, 2, 3], [4, 5, 6]]

The link trap in Python πŸ”—πŸ•³οΈ

When you assign a list to another variable (A = B) or make a regular slice (A = B[:]), Python doesn't physically copy the data. It simply creates a new reference to the same objects in memory. If the list contains other mutable objects (lists, dictionaries, custom classes), standard copying methods will only create a shallow copy. The copy module allows you to control this process.

β€” Breaking the links: The deepcopy function recursively traverses the entire data structure and creates honest, independent duplicates for each nested element. This ensures that changes in the copy will not harm the original data. πŸ”“πŸ”’
β€” Safe state: The use of deep copying is critical when implementing design patterns (for example, Snapshot/Memento), creating game state backups, or when you pass complex configurations to functions that may modify them accidentally. πŸ›‘οΈπŸ’Ύ
β€” A sensible balance: It's worth remembering that deepcopy works slower and consumes more memory than shallow copying, as it spends resources on creating new objects and checking for cyclic references. Use it specifically when there are nested mutable containers within the structure. βš–οΈπŸ§ 

#Python #Programming #DeepCopy #Coding #Tech #Dev

✨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Telegram AI PYTHON 🌟 You’ve been invited to add the folder β€œAI PYTHON πŸŒŸβ€, which includes 15 chats.
  • ❀ 7
More from @pythonre
  1. Oct 7, 2026A distinctive bot for subscribing to courses on the most popular educational platforms acr…
  2. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  3. Oct 7, 2026πŸ“± 270 tools for Python developers The repository contains: 🫑 Linters and formatters; πŸ«‘β€¦
  4. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
  5. Oct 6, 2026Post #9851
  6. Oct 6, 2026Python For Beginners Course In-Depth Python: A Comprehensive Introduction to Fundamental P…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook β†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 β†’