Python - Shallow and Deep Copy

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 พ.ย. 2024

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

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

    Watch more Python Videos Here: th-cam.com/play/PLWPirh4EWFpHYP2n7zUlquPVnGPi5aID7.html&si=HwqNZJVPXnSnSxj3

  • @ALLINONE-fh8pw
    @ALLINONE-fh8pw 4 ปีที่แล้ว +4

    Shallow copy uses same same memory of old_list to new_list
    But Deep copy is creating new memory for new_list.
    Right?

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

    Thank you so much, your tutorials are fantastic. You have helped me so much with my studies. I really appreciate it. Best regards

    • @ALLINONE-fh8pw
      @ALLINONE-fh8pw 4 ปีที่แล้ว

      Shallow copy uses same same memory of old_list to new_list
      But Deep copy is creating new memory for new_list.
      Right?

  • @cricketcollections4336
    @cricketcollections4336 2 ปีที่แล้ว

    Nice explaination thank u so much sir

  • @pasupuletimadhuri4478
    @pasupuletimadhuri4478 3 ปีที่แล้ว

    Very nice explanation sir

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

    Dear Sir, I have one question about deepcopy if you look at the example below how is it that after deepcoy id of the value of two "different objects" after deepcopy the id of value are the same (line 8 and 9) so here it still seems to be the same element (value) from the list shared by two different objects:
    1. import copy
    2. old_list = [[1,2],[3,4],[5,6]]
    3. deep_list = copy.deepcopy(old_list)
    4. print(old_list, 'old_list - deepcopy')
    5. print(deep_list, 'deep_list')
    6. print(id(old_list), 'id old_list after deepcopy')
    7. print(id(deep_list), 'id deep_list')
    8. print(id(old_list[1][1]), 'id old_list [1][1]')
    9. print(id(deep_list[1][1]), 'id deep_list[1][1]')
    10. old_list[1][1] = 'BB'
    11. print(old_list, 'old_list - deepcopy')
    12. print(deep_list, 'deep_list')
    13. print(id(old_list), 'id old_list after deepcopy')
    14. print(id(deep_list), 'id deep_list')
    Output:
    [[1, 2], [3, 4], [5, 6]] old_list
    [[1, 2], [3, 4], [5, 6]] deep_list
    3419752 id old_list after deepcopy
    3778640 id deep_list
    267442400 id old_list [1][1]
    267442400 id deep_list[1][1]
    [[1, 2], [3, 'BB'], [5, 6]] old_list - deepcopy
    [[1, 2], [3, 4], [5, 6]] deep_list
    3419752 id old_list after deepcopy
    3778640 id deep_list
    Thank you in advance

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

    Need to learn to explain

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

    why do people use shallow copy? what are its uses, if its not doing recursively?

    • @ind-ram
      @ind-ram 4 ปีที่แล้ว

      in terms of space complexity we use shallow copy as it just copies items of original object as references to copied object

    • @ALLINONE-fh8pw
      @ALLINONE-fh8pw 4 ปีที่แล้ว

      @@ind-ram Shallow copy uses same same memory of old_list to new_list
      But Deep copy is creating new memory for new_list.
      Right?

    • @ind-ram
      @ind-ram 4 ปีที่แล้ว

      @@ALLINONE-fh8pw exactly

  • @PeacefulMusic-l7o
    @PeacefulMusic-l7o ปีที่แล้ว

    I have a doubt, why is [4, 4, 4] not appended to the new_list ??

  • @ashishmaurya3113
    @ashishmaurya3113 5 ปีที่แล้ว

    what are nested objects?

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

      list inside list r called nested list

  • @MrAbhishekparida
    @MrAbhishekparida 6 ปีที่แล้ว

    BEST

  • @Explorer_Ajay
    @Explorer_Ajay 5 ปีที่แล้ว

    Not clear

  • @anonymous_69-c8k
    @anonymous_69-c8k 5 ปีที่แล้ว

    cha cha har cheez p video bnana zruri nhe h ......