Your Lists Are Being Copied WRONG In Python (Shallow Copy VS. Deep Copy)

preview_player
Показать описание
This is actually really important in Python, and I see a lot of people using copy() as if they're creating a deep copy of the original list, but that isn't the case, and I'll explain everything you need to know about it in Python.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

I noticed I that my type annotations in the video were wrong after recording… I meant to write:

a: list[list[int]]
B: list[list[int]]

It does not change anything regarding the lesson though :)

Indently
Автор

I would like to hear about the potential disadvantages of using deepcopy such as higher memory usage or speed reduction

martinsz
Автор

The case when at language level you come up with a lot of simplifications but under the hood there are still values, references and types. They abstract it all the way they could but when it comes to actually understanding what’s going on you still need to understand how it actually works. Otherwise you would hear explanations like “1st level deep copies” awful, although the explanation pretty much valid by fact it’s not fully correct by idea.

kamurashev
Автор

This was one thing i noticed while programming in python and working with lists but i figured it out eventuelly but i never undestood how the basic copy() works, I only used deepcopy. Thanks for explaining

dzendys_
Автор

one crazy way to make independent (yet shallow) copy is to reverse a reversed list like b = a[::-1][::-1]
try
a = [1, 2, 3]
b = a[::-1][::-1]
b.append(4)
print(a) # [1, 2, 3]

But remember it is still a shallow copy not a deep copy

xzex
Автор

I really feel like this video should address the fundamentals of references and mutability in Python. These concepts aren't unique to Python by any means, and the concepts have implications in Python outside of copying lists.

Sevalecan
Автор

OMG!, such an important gotcha and never heard of it

lokeshkalamalla
Автор

Great illustration. This can be a difficult bug to squash.

JasonEmanuel
Автор

Totally went down that rabbit hole a few months ago…..thanks for explanation…

Normie_dog
Автор

so deepcopy is like a lambda, which also gets a new memory address for variables. Python really forces you to think about why you need that copy. Given global access, I like that actually.

greycell
Автор

this is not limited to lists, dictionaries and tuples got the same id when you assign them to a new variable ( copy method works as it should )

xzex
Автор

Literally just made this mistake today haha! I think it would be good to explain how this works at a bit of a deeper level for clarity (i.e it's because a list is just a reference)

mrxcreamy
Автор

i was really confused when i first noticed that, it's maybe the first languages that i found does this

xxxxxxx
Автор

Gosh. I didnt know that. Useful. Thank you.

loverboykimi
Автор

I think i accually had problem with it yesterday. Thanks!

Oler-yxxj
Автор

Thank you, this was just what I was looking for!

jensmunkerud
Автор

What if you make a copy of list a with slicing? i.e b = a[:]

therollingambit
Автор

So basically, if I understood right, when you do a shallow copy the lists are separate objects but the items currently inside them are the same?

bettercalldelta
Автор

one way to make a shallow ( just one level copy) independent copy is to reverse a reversed list b = a [ : :-1][: :-1]

xzex
Автор

How to solve the problem:
Copy() is a shallow level of copy, but if you want to make a deep copy just import copy library and overwrite copy() per copy.deepcopy()

jangoul