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

  Рет қаралды 19,576

Indently

Indently

Жыл бұрын

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:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels

Пікірлер: 66
@Indently
@Indently Жыл бұрын
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 :)
@wartem
@wartem Жыл бұрын
Maybe just skip it entirely for better readability.
@tinahalder8416
@tinahalder8416 Жыл бұрын
@@wartem nope, type hinting helps with code readability
@wartem
@wartem Жыл бұрын
@@tinahalder8416 Not by default. Not in every situation. In this case it clutters the code. But that's subjective I guess. Less is more.
@Indently
@Indently Жыл бұрын
It's a preference, so I understand exactly what I'm working with. list[list[Int]] is probably as far as I would go with that though, otherwise I completely agree, list[list[list[list[int]]] would look hilarious.
@xzex2609
@xzex2609 Жыл бұрын
I never understand why some people declare type , never ever , the language itself let us all free of this nonsense and you use it for linters to show you some more methods that you already must know all of them ?
@martinsz441
@martinsz441 Жыл бұрын
I would like to hear about the potential disadvantages of using deepcopy such as higher memory usage or speed reduction
@Indently
@Indently Жыл бұрын
It is tougher on memory for sure.
@dzendys_
@dzendys_ Жыл бұрын
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
@kamurashev
@kamurashev Жыл бұрын
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.
@xzex2609
@xzex2609 Жыл бұрын
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
@samvelsafaryan4698
@samvelsafaryan4698 Жыл бұрын
Hi bro, what ide do you use?
@Sevalecan
@Sevalecan 4 ай бұрын
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.
@jensmunkerud
@jensmunkerud 8 ай бұрын
Thank you, this was just what I was looking for!
@KonradTamas
@KonradTamas 11 ай бұрын
Always Informative and to the point.
@Normie_dog
@Normie_dog Жыл бұрын
Totally went down that rabbit hole a few months ago…..thanks for explanation…
@Oler-yx7xj
@Oler-yx7xj Жыл бұрын
I think i accually had problem with it yesterday. Thanks!
@greycell2442
@greycell2442 Жыл бұрын
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.
@frd85
@frd85 4 ай бұрын
very important topic! nice video!
@xzex2609
@xzex2609 Жыл бұрын
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 )
@xxxxxxx38
@xxxxxxx38 4 ай бұрын
i was really confused when i first noticed that, it's maybe the first languages that i found does this
@loverboykimi
@loverboykimi Жыл бұрын
Gosh. I didnt know that. Useful. Thank you.
@mrxcreamy10
@mrxcreamy10 Жыл бұрын
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)
@JasonEmanuel
@JasonEmanuel Жыл бұрын
Great illustration. This can be a difficult bug to squash.
@kychemclass5850
@kychemclass5850 Жыл бұрын
Tq for sharing this knowledge.
@ChrisHalden007
@ChrisHalden007 3 ай бұрын
Great video. Thanks
@lokeshkalamalla
@lokeshkalamalla Жыл бұрын
OMG!, such an important gotcha and never heard of it
@neerubhardwaj2773
@neerubhardwaj2773 10 ай бұрын
Thanks for sharing
@bettercalldelta
@bettercalldelta Жыл бұрын
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?
@nopinias69
@nopinias69 Жыл бұрын
Yes, you could add another list to one of those list of lists and it wouldn't affect the other one... But if you modify one item inside one list (that is inside the list of lists) that item will be modified in the others lists (that also are inside of a list of lists). Sorry for my bad English and also because it's confusing the way I wrote this...
@LAB_XI
@LAB_XI 5 ай бұрын
Thank you ❤
@layan2524
@layan2524 3 ай бұрын
Thanks!!!
@alidev425
@alidev425 Жыл бұрын
Useful tips thanks,btw nice haircut👍
@Indently
@Indently Жыл бұрын
Oh thank you! Ahah
@realcontentgamer
@realcontentgamer Жыл бұрын
Happy new (early) Year!
@alexnoneofyourbuisness
@alexnoneofyourbuisness 10 ай бұрын
Thanx you are the best👑
@therollingambit5222
@therollingambit5222 Жыл бұрын
What if you make a copy of list a with slicing? i.e b = a[:]
@Indently
@Indently Жыл бұрын
Why don't you add .copy to a[:]? So: "a[:].copy()".
@Indently
@Indently Жыл бұрын
Good to know!
@sasa-bv9gu
@sasa-bv9gu Жыл бұрын
@@Indently yeah and reverse it and add deepcopy why not b=copy.deepcopy(a[:][::-1][::-1].copy())
@jangoul
@jangoul Жыл бұрын
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()
@xzex2609
@xzex2609 Жыл бұрын
one way to make a shallow ( just one level copy) independent copy is to reverse a reversed list b = a [ : :-1][: :-1]
@Indently
@Indently Жыл бұрын
I like the enthusiasm, but you could also just do a[:] and it would have made a shallow copy too
@xzex2609
@xzex2609 Жыл бұрын
@@Indently yes actually slicing works better , thanx for the note
@Indently
@Indently Жыл бұрын
But keep up the creativity! I love seeing those random snippets
@xzex2609
@xzex2609 Жыл бұрын
@@Indently actually I couldn't remember exactly the slicing, but somehow I remember that some variation that I did apply to get the different id, thanx again for your valuable videos, it's really a great way to learn details that is in documentations.
@thejohannesbeck
@thejohannesbeck Жыл бұрын
Having to use copy in the first place is often a sign for bad code design overall in my opinion
@Indently
@Indently Жыл бұрын
I'm not challenging your opinion, just curious to hear more about it, if you don't mind sharing? 😁
@mordechaidaniel7455
@mordechaidaniel7455 Жыл бұрын
Couldn't you just do [x for x in a]?
@Sinke_100
@Sinke_100 Жыл бұрын
tryed, does the same thing. I really didn't know, this could also do the trick np.array(a.copy()).tolist()
@callbettersaul
@callbettersaul Жыл бұрын
What if you have 4 levels deep list? Annoying to do it that way then, wouldn't it?
@Sinke_100
@Sinke_100 Жыл бұрын
@@callbettersaul I saw deprecation warning, interesthing, you have to specify it as an object array
@murphygreen8484
@murphygreen8484 Жыл бұрын
It's too bad all copies are not deep copies natively.
@검불그스름
@검불그스름 Жыл бұрын
That's actually the beauty and benefit of Python. Why give up the most valuable feature of Python just to make beginners happy?
@kamurashev
@kamurashev Жыл бұрын
@@검불그스름 and if you would really do the things you just said it wouldn’t be python. It would be a normal language 🙂 cause in all the other aspects it’s the same trade of- make it stupid and awful but novices seem to be happy, at least at first.
@tinahalder8416
@tinahalder8416 Жыл бұрын
Because, well, most of the time we don't need deep copy. Shallow copy saves on memory
@검불그스름
@검불그스름 Жыл бұрын
@@tinahalder8416 Exactly. Most of the time shallow copy (or assignment by reference) saves time and space
@sasa-bv9gu
@sasa-bv9gu Жыл бұрын
guys, just in case, i recommend using this method to make copied lists, just in case. (no memory leaks or whatever) b=copy.deepcopy(a[:][::-1][::-1].copy())
@Indently
@Indently Жыл бұрын
I don't understand any of this
@sasa-bv9gu
@sasa-bv9gu Жыл бұрын
@@Indently just a goof
@mrmuranga
@mrmuranga Жыл бұрын
thanks for sharing
ALL 11 LIST METHODS IN PYTHON EXPLAINED
9:23
Indently
Рет қаралды 78 М.
Каха и суп
00:39
К-Media
Рет қаралды 2,7 МЛН
DO YOU HAVE FRIENDS LIKE THIS?
00:17
dednahype
Рет қаралды 85 МЛН
Python Shallow Copy and Deep Copy Tutorial
14:21
Programming Is Fun
Рет қаралды 2,5 М.
Python: Deep Copy vs Shallow Copy
7:59
Oggi AI - Artificial Intelligence Today
Рет қаралды 1,7 М.
5 советов по написанию функций Python
12:44
David Arzumanyan
Рет қаралды 1,2 М.
20 Everyday Tips & Tricks in Python
25:18
Indently
Рет қаралды 16 М.
ALL 47 STRING METHODS IN PYTHON EXPLAINED
23:34
Indently
Рет қаралды 120 М.
WHY IS THE HEAP SO SLOW?
17:53
Core Dumped
Рет қаралды 208 М.
Rust Data Modelling Without Classes
11:25
No Boilerplate
Рет қаралды 165 М.
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 101 М.