r/PythonLearning • u/Sea-Ad7805 • Apr 15 '26
What's the difference between List copies in Python?
An exercise to help build the right mental model for Python data.
The โSolutionโ link visualizes execution and reveals whatโs actually happening using ๐บ๐ฒ๐บ๐ผ๐ฟ๐_๐ด๐ฟ๐ฎ๐ฝ๐ต.
2
6
u/Alive-Cake-3045 Apr 17 '26
Shallow copy is like copying a folder shortcut, the files inside still point to the same place.
a = [[1, 2], [3, 4]] b = a.copy() b[0][0] = 99 print(a) # [[99, 2], [3, 4]] -- a changed too!
Deep copy is copying everything, the folder and every file inside it, fully independent.
import copy b = copy.deepcopy(a)
Flat list with no nesting? Just use b = a[:] and move on.
1
Apr 15 '26
[removed] โ view removed comment
1
u/Sea-Ad7805 Apr 15 '26 edited Apr 15 '26
Let me explain the duplicates:
c2 = a[:] # slicing operator, try: a=list(range(10)); b=a[1:5] c3 = list(a) # conversion operator, try: a=list( set(1,2,3) ) c4 = a.copy() # shallow copy method specific to list type c5 = copy.copy(a) # shallow copy for any type including listMy understanding is that for a deep copy in Perl you also need an import, something like:
use Storable qw(dclone); use Clone qw(clone);I'm sorry you're a Perl developer, but it's never too late to change from a scripting language to a real language. If you don't like Python, try C++, Java or Rust. Good luck.
2
u/IndependentAnswer129 Apr 15 '26
"Scripting language." You just called Perl a scripting language. My scraper pulls five news sites into a CSV file every morning at 7 AM while I sleep. That's not a script. That's an employee. An employee that never calls in sick.
"It's never too late to change to a real language." Perl has been real since 1987. I don't know when C++ was born but it sounds like a grade you get when you almost failed. Java sounds like coffee. Rust is what happens to metal when you leave it outside. These are not serious names.
I don't know what dclone or Storable is. I haven't needed them. When I need to copy something I copy it. If that stops working I'll deal with it then. I've been coding for eleven days and nothing has stopped working yet. Can you say the same?
Brad Newsworthy
2
u/Sea-Ad7805 Apr 15 '26 edited Apr 16 '26
Maybe look into the 'deep copy' concept, it's important to avoid bugs when using nested data. This is a Perl example ChatGPT gave me, hope it helps you:
use strict; use warnings; use Storable qw(dclone); my $original = [ [1, 2], [3, 4], ]; my $shallow = [ @$original ]; # shallow copy: same outer array, new inner arrays my $deep = dclone($original); # deep copy: fully copied print "Before change:\n"; show_all($original, $shallow, $deep); $shallow->[0][0] = 99; $deep->[1][1] = 88; print "\nAfter change:\n"; show_all($original, $shallow, $deep); sub show_all { my ($original, $shallow, $deep) = @_; print "original = ", format_2d($original), "\n"; print "shallow = ", format_2d($shallow), "\n"; print "deep = ", format_2d($deep), "\n"; } sub format_2d { my ($a) = @_; return "[" . join(", ", map { "[" . join(", ", @$_) . "]" } @$a ) . "]"; }
โข
u/Sea-Ad7805 Apr 15 '26
Solution: https://memory-graph.com/#codeurl=https://raw.githubusercontent.com/bterwijn/memory_graph_videos/refs/heads/main/exercises/exercise12.py&play
Explanation: https://github.com/bterwijn/memory_graph?tab=readme-ov-file#python-data-model
More exercises: https://www.reddit.com/r/Python_memory_graph/