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

Поділитися
Вставка
  • Опубліковано 22 гру 2024

КОМЕНТАРІ •

  • @Indently
    @Indently  2 роки тому +23

    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 2 роки тому +1

      Maybe just skip it entirely for better readability.

    • @tinahalder8416
      @tinahalder8416 2 роки тому +3

      @@wartem nope, type hinting helps with code readability

    • @wartem
      @wartem 2 роки тому +3

      @@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  2 роки тому +5

      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 Рік тому +2

      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 2 роки тому +6

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

    • @Indently
      @Indently  2 роки тому +2

      It is tougher on memory for sure.

  • @kamurashev
    @kamurashev 2 роки тому +12

    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.

  • @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

  • @xzex2609
    @xzex2609 Рік тому +1

    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

  • @bendirval3612
    @bendirval3612 15 днів тому +1

    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.

  • @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 )

  • @零云-u7e
    @零云-u7e 2 роки тому +1

    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.

  • @samvelsafaryan4698
    @samvelsafaryan4698 Рік тому +1

    Hi bro, what ide do you use?

  • @Nate_dog01
    @Nate_dog01 2 роки тому

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

  • @therollingambit5222
    @therollingambit5222 2 роки тому +2

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

    • @Indently
      @Indently  2 роки тому

      Why don't you add .copy to a[:]? So: "a[:].copy()".

    • @Indently
      @Indently  2 роки тому +1

      Good to know!

    • @ob0-china
      @ob0-china Рік тому

      @@Indently yeah and reverse it and add deepcopy why not
      b=copy.deepcopy(a[:][::-1][::-1].copy())

  • @JasonEmanuel
    @JasonEmanuel Рік тому

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

  • @Sevalecan
    @Sevalecan 9 місяців тому

    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.

  • @lokeshkalamalla
    @lokeshkalamalla 2 роки тому +2

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

  • @Oler-yx7xj
    @Oler-yx7xj 2 роки тому

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

  • @bettercalldelta
    @bettercalldelta 2 роки тому

    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 2 роки тому

      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
    @jensmunkerud Рік тому

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

  • @loverboykimi
    @loverboykimi 2 роки тому

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

  • @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  Рік тому +1

      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.

  • @mrxcreamy10
    @mrxcreamy10 2 роки тому

    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)

  • @KonradTamas
    @KonradTamas Рік тому

    Always Informative and to the point.

  • @youssefsh333
    @youssefsh333 Місяць тому

    what code editor is this

  • @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()

  • @frd85
    @frd85 9 місяців тому

    very important topic! nice video!

  • @alidev425
    @alidev425 2 роки тому

    Useful tips thanks,btw nice haircut👍

    • @Indently
      @Indently  2 роки тому

      Oh thank you! Ahah

  • @kychemclass5850
    @kychemclass5850 Рік тому

    Tq for sharing this knowledge.

  • @realcontentgamer
    @realcontentgamer 2 роки тому

    Happy new (early) Year!

  • @ChrisHalden007
    @ChrisHalden007 9 місяців тому

    Great video. Thanks

  • @LAB_XI
    @LAB_XI 11 місяців тому

    Thank you ❤

  • @mordechaidaniel7455
    @mordechaidaniel7455 2 роки тому

    Couldn't you just do [x for x in a]?

    • @Sinke_100
      @Sinke_100 2 роки тому

      tryed, does the same thing. I really didn't know, this could also do the trick np.array(a.copy()).tolist()

    • @callbettersaul
      @callbettersaul 2 роки тому

      What if you have 4 levels deep list? Annoying to do it that way then, wouldn't it?

    • @Sinke_100
      @Sinke_100 2 роки тому

      @@callbettersaul I saw deprecation warning, interesthing, you have to specify it as an object array

  • @a.a.b.f.a
    @a.a.b.f.a Рік тому

    Thanx you are the best👑

  • @ob0-china
    @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
      @Indently  Рік тому

      I don't understand any of this

    • @ob0-china
      @ob0-china Рік тому

      @@Indently just a goof

  • @neerubhardwaj2773
    @neerubhardwaj2773 Рік тому

    Thanks for sharing

  • @mrmuranga
    @mrmuranga Рік тому

    thanks for sharing

  • @layan2524
    @layan2524 9 місяців тому

    Thanks!!!

  • @thejohannesbeck
    @thejohannesbeck 2 роки тому

    Having to use copy in the first place is often a sign for bad code design overall in my opinion

    • @Indently
      @Indently  2 роки тому

      I'm not challenging your opinion, just curious to hear more about it, if you don't mind sharing? 😁

  • @sanskarpiya5368
    @sanskarpiya5368 4 місяці тому

    ohhhhhhhhhhh
    i understanded nothing

  • @murphygreen8484
    @murphygreen8484 2 роки тому

    It's too bad all copies are not deep copies natively.

    • @검불그스름
      @검불그스름 2 роки тому +4

      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 2 роки тому

      @@검불그스름 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 2 роки тому +4

      Because, well, most of the time we don't need deep copy. Shallow copy saves on memory

    • @검불그스름
      @검불그스름 2 роки тому +1

      @@tinahalder8416 Exactly. Most of the time shallow copy (or assignment by reference) saves time and space