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

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 พ.ย. 2024
  • 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

ความคิดเห็น • 67

  • @Indently
    @Indently  ปีที่แล้ว +22

    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 ปีที่แล้ว +1

      Maybe just skip it entirely for better readability.

    • @tinahalder8416
      @tinahalder8416 ปีที่แล้ว +3

      @@wartem nope, type hinting helps with code readability

    • @wartem
      @wartem ปีที่แล้ว +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  ปีที่แล้ว +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 ?

  • @kamurashev
    @kamurashev ปีที่แล้ว +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.

  • @martinsz441
    @martinsz441 ปีที่แล้ว +6

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

    • @Indently
      @Indently  ปีที่แล้ว +2

      It is tougher on memory for sure.

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

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

  • @SoulExpension
    @SoulExpension ปีที่แล้ว +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.

  • @Nate_dog01
    @Nate_dog01 ปีที่แล้ว

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

  • @Sevalecan
    @Sevalecan 8 หลายเดือนก่อน

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

  • @JasonEmanuel
    @JasonEmanuel ปีที่แล้ว

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

  • @lokeshkalamalla
    @lokeshkalamalla ปีที่แล้ว +2

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

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

  • @Oler-yx7xj
    @Oler-yx7xj ปีที่แล้ว

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

  • @jensmunkerud
    @jensmunkerud ปีที่แล้ว

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

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

  • @loverboykimi
    @loverboykimi ปีที่แล้ว

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

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

  • @samvelsafaryan4698
    @samvelsafaryan4698 ปีที่แล้ว +1

    Hi bro, what ide do you use?

  • @KonradTamas
    @KonradTamas ปีที่แล้ว

    Always Informative and to the point.

  • @realcontentgamer
    @realcontentgamer ปีที่แล้ว

    Happy new (early) Year!

  • @kychemclass5850
    @kychemclass5850 ปีที่แล้ว

    Tq for sharing this knowledge.

  • @frd85
    @frd85 8 หลายเดือนก่อน

    very important topic! nice video!

  • @LAB_XI
    @LAB_XI 9 หลายเดือนก่อน

    Thank you ❤

  • @ChrisHalden007
    @ChrisHalden007 7 หลายเดือนก่อน

    Great video. Thanks

  • @therollingambit5222
    @therollingambit5222 ปีที่แล้ว +2

    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  ปีที่แล้ว +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())

  • @layan2524
    @layan2524 7 หลายเดือนก่อน

    Thanks!!!

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

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

  • @mrmuranga
    @mrmuranga ปีที่แล้ว

    thanks for sharing

  • @neerubhardwaj2773
    @neerubhardwaj2773 ปีที่แล้ว

    Thanks for sharing

  • @alidev425
    @alidev425 ปีที่แล้ว

    Useful tips thanks,btw nice haircut👍

    • @Indently
      @Indently  ปีที่แล้ว

      Oh thank you! Ahah

  • @alexnoneofyourbuisness
    @alexnoneofyourbuisness ปีที่แล้ว

    Thanx you are the best👑

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

  • @sanskarpiya5368
    @sanskarpiya5368 2 หลายเดือนก่อน

    ohhhhhhhhhhh
    i understanded nothing

  • @murphygreen8484
    @murphygreen8484 ปีที่แล้ว

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

    • @검불그스름
      @검불그스름 ปีที่แล้ว +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 ปีที่แล้ว

      @@검불그스름 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 ปีที่แล้ว +4

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

    • @검불그스름
      @검불그스름 ปีที่แล้ว +1

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