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 :)
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.
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 ?
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.
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
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
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.
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.
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...
@@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.
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)
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()
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())
@@검불그스름 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.
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 :)
Maybe just skip it entirely for better readability.
@@wartem nope, type hinting helps with code readability
@@tinahalder8416 Not by default. Not in every situation. In this case it clutters the code. But that's subjective I guess. Less is more.
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.
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 ?
I would like to hear about the potential disadvantages of using deepcopy such as higher memory usage or speed reduction
It is tougher on memory for sure.
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.
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
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
I don't understand why there isn't a deep copy in base python. I don't want to have to import a module any time I want to copy a list.
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 )
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.
Hi bro, what ide do you use?
Totally went down that rabbit hole a few months ago…..thanks for explanation…
What if you make a copy of list a with slicing? i.e b = a[:]
Why don't you add .copy to a[:]? So: "a[:].copy()".
Good to know!
@@Indently yeah and reverse it and add deepcopy why not
b=copy.deepcopy(a[:][::-1][::-1].copy())
Great illustration. This can be a difficult bug to squash.
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.
OMG!, such an important gotcha and never heard of it
I think i accually had problem with it yesterday. Thanks!
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?
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...
Thank you, this was just what I was looking for!
Gosh. I didnt know that. Useful. Thank you.
one way to make a shallow ( just one level copy) independent copy is to reverse a reversed list b = a [ : :-1][: :-1]
I like the enthusiasm, but you could also just do a[:] and it would have made a shallow copy too
@@Indently yes actually slicing works better , thanx for the note
But keep up the creativity! I love seeing those random snippets
@@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.
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)
Always Informative and to the point.
what code editor is this
PyCharm
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()
very important topic! nice video!
Useful tips thanks,btw nice haircut👍
Oh thank you! Ahah
Tq for sharing this knowledge.
Happy new (early) Year!
Great video. Thanks
Thank you ❤
Couldn't you just do [x for x in a]?
tryed, does the same thing. I really didn't know, this could also do the trick np.array(a.copy()).tolist()
What if you have 4 levels deep list? Annoying to do it that way then, wouldn't it?
@@callbettersaul I saw deprecation warning, interesthing, you have to specify it as an object array
Thanx you are the best👑
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())
I don't understand any of this
@@Indently just a goof
Thanks for sharing
thanks for sharing
Thanks!!!
Having to use copy in the first place is often a sign for bad code design overall in my opinion
I'm not challenging your opinion, just curious to hear more about it, if you don't mind sharing? 😁
ohhhhhhhhhhh
i understanded nothing
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?
@@검불그스름 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.
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