Beginner Python Tutorial 36 - How to Deep Copy a List (copy.deepcopy)

Beginner Python Tutorial 36 - How to Deep Copy a List (copy.deepcopy)

Caleb Curry

4 года назад

6,458 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

menmer
menmer - 04.08.2022 00:55

import copy

nations = [["United States",["Texas", "California"], "Canada",["British Columbia", "Saskatchewan"] ],
["Mexico",["Hidalgo", "Jalisco"], "Panama",["Bocas del Toro", "Herrera"]],
["United Kingdom",['Wales', 'Scotland'], "Germany",["Bavaria", "Saxony"]]]

nation_copy= copy.deepcopy(nations)

print(id(nations[0][1][0]), id(nation_copy[0][1][0]))

it prints: 1729294595056 1729294595056

import copy

nations = [["United States",["Texas", "California"], "Canada",["British Columbia", "Saskatchewan"] ],
["Mexico",["Hidalgo", "Jalisco"], "Panama",["Bocas del Toro", "Herrera"]],
["United Kingdom",['Wales', 'Scotland'], "Germany",["Bavaria", "Saxony"]]]

nation_copy = copy.deepcopy(nations)
nation_copy[0][0] = "USA"
print(id(nations[0][1][0]), id(nation_copy[0][1][0]))
print(nations)
print(nation_copy)

it prints:
2679814058992 2679814058992
[['United States', ['Texas', 'California'], 'Canada', ['British Columbia', 'Saskatchewan']], ['Mexico', ['Hidalgo', 'Jalisco'], 'Panama', ['Bocas del Toro', 'Herrera']], ['United Kingdom', ['Wales', 'Scotland'], 'Germany', ['Bavaria', 'Saxony']]]
[['USA', ['Texas', 'California'], 'Canada', ['British Columbia', 'Saskatchewan']], ['Mexico', ['Hidalgo', 'Jalisco'], 'Panama', ['Bocas del Toro', 'Herrera']], ['United Kingdom', ['Wales', 'Scotland'], 'Germany', ['Bavaria', 'Saxony']]]

the IDs are the same but when i change something from the copy it doesn't affect the original.
why does this happen?

Ответить
Nahom Elias
Nahom Elias - 23.10.2021 19:11

I have killed hours trying to understand, why is it changing my original list, you just explained it with 5 minutes, tnx alot

Ответить
Stephen Kurni Cloud
Stephen Kurni Cloud - 18.02.2021 01:05

Not only the video is explainin HOW to deep copy, it also explains the different clearly!

Ответить
McSkiletBiscuit
McSkiletBiscuit - 26.08.2020 06:44

This was perfect for a problem I was having. Thank you!

Ответить
Miki Jasar
Miki Jasar - 07.06.2020 22:59

Dear Curry, Thank you so much for your effort but I have one question and if you find the time I would ask you to answer me regarding deep.copy ( ):
How can you explain that in the example below after deep.copy ( ) id from print(id(list_1[0][1])) print(id(c_list_1[0][1])) are the same which means they are the same objects??
import copy
list_1 = [[1,2],[3,4]]
c_list_1 = copy.deepcopy(list_1)
print(list_1, c_list_1)
print(id(list_1[0][1]), 'id list_1[0][1]')
print(id(c_list_1[0][1]), 'id c_list_1[0][1]')

___________________
[[1, 2], [3, 4]] [[1, 2], [3, 4]]
1731516608 id list_1[0][1]
1731516608 id c_list_1[0][1]

Thanks in advance

Ответить
Firdaus Azad
Firdaus Azad - 23.04.2020 23:52

.copy() isn't changing the original but deepcopy does right ?

Ответить
Firdaus Azad
Firdaus Azad - 23.04.2020 23:46

That's mean the value of first list is going to change just when we have nested list and we change a member of the nested list ?

Ответить
KiiDFLiiGHT
KiiDFLiiGHT - 23.04.2020 13:08

This is great because I have a hard time literally building a foundation

Ответить