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 Жыл бұрын
Maybe just skip it entirely for better readability.
@tinahalder8416 Жыл бұрын
@@wartem nope, type hinting helps with code readability
@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 Жыл бұрын
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 Жыл бұрын
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 ?
@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.
@martinsz441 Жыл бұрын
I would like to hear about the potential disadvantages of using deepcopy such as higher memory usage or speed reduction
@Indently Жыл бұрын
It is tougher on memory for sure.
@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
@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
@SoulExpension Жыл бұрын
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.
@Sevalecan8 ай бұрын
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.
@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 )
@Nate_dog01 Жыл бұрын
Totally went down that rabbit hole a few months ago…..thanks for explanation…
@JasonEmanuel Жыл бұрын
Great illustration. This can be a difficult bug to squash.
@lokeshkalamalla Жыл бұрын
OMG!, such an important gotcha and never heard of it
@Oler-yx7xj Жыл бұрын
I think i accually had problem with it yesterday. Thanks!
@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()
@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)
@loverboykimi Жыл бұрын
Gosh. I didnt know that. Useful. Thank you.
@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 Жыл бұрын
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...
@jensmunkerud Жыл бұрын
Thank you, this was just what I was looking for!
@xzex2609 Жыл бұрын
one way to make a shallow ( just one level copy) independent copy is to reverse a reversed list b = a [ : :-1][: :-1]
@Indently Жыл бұрын
I like the enthusiasm, but you could also just do a[:] and it would have made a shallow copy too
@xzex2609 Жыл бұрын
@@Indently yes actually slicing works better , thanx for the note
@Indently Жыл бұрын
But keep up the creativity! I love seeing those random snippets
@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.
@samvelsafaryan4698 Жыл бұрын
Hi bro, what ide do you use?
@KonradTamas Жыл бұрын
Always Informative and to the point.
@therollingambit5222 Жыл бұрын
What if you make a copy of list a with slicing? i.e b = a[:]
@Indently Жыл бұрын
Why don't you add .copy to a[:]? So: "a[:].copy()".
@Indently Жыл бұрын
Good to know!
@ob0-china Жыл бұрын
@@Indently yeah and reverse it and add deepcopy why not b=copy.deepcopy(a[:][::-1][::-1].copy())
@realcontentgamer Жыл бұрын
Happy new (early) Year!
@frd859 ай бұрын
very important topic! nice video!
@alidev425 Жыл бұрын
Useful tips thanks,btw nice haircut👍
@Indently Жыл бұрын
Oh thank you! Ahah
@ChrisHalden0078 ай бұрын
Great video. Thanks
@kychemclass5850 Жыл бұрын
Tq for sharing this knowledge.
@LAB_XI10 ай бұрын
Thank you ❤
@ob0-china Жыл бұрын
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 Жыл бұрын
I don't understand any of this
@ob0-china Жыл бұрын
@@Indently just a goof
@thejohannesbeck Жыл бұрын
Having to use copy in the first place is often a sign for bad code design overall in my opinion
@Indently Жыл бұрын
I'm not challenging your opinion, just curious to hear more about it, if you don't mind sharing? 😁
@youssefsh33311 күн бұрын
what code editor is this
@Indently10 күн бұрын
PyCharm
@layan25248 ай бұрын
Thanks!!!
@neerubhardwaj2773 Жыл бұрын
Thanks for sharing
@mrmuranga Жыл бұрын
thanks for sharing
@a.a.b.f.a Жыл бұрын
Thanx you are the best👑
@mordechaidaniel7455 Жыл бұрын
Couldn't you just do [x for x in a]?
@Sinke_100 Жыл бұрын
tryed, does the same thing. I really didn't know, this could also do the trick np.array(a.copy()).tolist()
@callbettersaul Жыл бұрын
What if you have 4 levels deep list? Annoying to do it that way then, wouldn't it?
@Sinke_100 Жыл бұрын
@@callbettersaul I saw deprecation warning, interesthing, you have to specify it as an object array
@sanskarpiya53683 ай бұрын
ohhhhhhhhhhh i understanded nothing
@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 Жыл бұрын
@@검불그스름 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 Жыл бұрын
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