[{"data":1,"prerenderedAt":1933},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-shallow-copy-vs-deep-copy-explained":3},{"id":4,"title":5,"body":6,"description":1926,"extension":1927,"meta":1928,"navigation":81,"path":1929,"seo":1930,"stem":1931,"__hash__":1932},"content\u002Freference\u002Fpython-shallow-copy-vs-deep-copy-explained.md","Python shallow copy vs deep copy Explained",{"type":7,"value":8,"toc":1901},"minimark",[9,13,17,20,33,45,48,53,254,267,271,288,292,302,305,316,319,424,430,433,529,539,547,551,556,558,568,570,679,687,691,694,697,714,720,805,808,812,815,820,823,886,889,893,896,992,996,999,1107,1111,1118,1149,1152,1240,1255,1259,1262,1273,1276,1361,1364,1371,1375,1378,1389,1392,1543,1546,1550,1553,1567,1570,1636,1643,1647,1650,1674,1677,1786,1789,1823,1827,1831,1834,1838,1841,1845,1848,1852,1855,1859,1862,1866,1897],[10,11,5],"h1",{"id":12},"python-shallow-copy-vs-deep-copy-explained",[14,15,16],"p",{},"When you copy data in Python, the result is not always fully independent from the original.",[14,18,19],{},"This is especially important with nested data, such as:",[21,22,23,27,30],"ul",{},[24,25,26],"li",{},"a list inside another list",[24,28,29],{},"a dictionary containing lists",[24,31,32],{},"a list of dictionaries",[14,34,35,36,40,41,44],{},"The difference between ",[37,38,39],"strong",{},"shallow copy"," and ",[37,42,43],{},"deep copy"," is about what gets copied and what stays shared.",[14,46,47],{},"If you get this wrong, changing one object can unexpectedly change another.",[49,50,52],"h2",{"id":51},"quick-example","Quick example",[54,55,60],"pre",{"className":56,"code":57,"language":58,"meta":59,"style":59},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import copy\n\noriginal = [[1, 2], [3, 4]]\nshallow = copy.copy(original)\ndeep = copy.deepcopy(original)\n\noriginal[0].append(99)\n\nprint(original)  # [[1, 2, 99], [3, 4]]\nprint(shallow)   # [[1, 2, 99], [3, 4]]\nprint(deep)      # [[1, 2], [3, 4]]\n","python","",[61,62,63,76,83,124,151,172,177,201,206,224,239],"code",{"__ignoreMap":59},[64,65,68,72],"span",{"class":66,"line":67},"line",1,[64,69,71],{"class":70},"sVHd0","import",[64,73,75],{"class":74},"su5hD"," copy\n",[64,77,79],{"class":66,"line":78},2,[64,80,82],{"emptyLinePlaceholder":81},true,"\n",[64,84,86,89,93,97,101,104,107,110,113,116,118,121],{"class":66,"line":85},3,[64,87,88],{"class":74},"original ",[64,90,92],{"class":91},"smGrS","=",[64,94,96],{"class":95},"sP7_E"," [[",[64,98,100],{"class":99},"srdBf","1",[64,102,103],{"class":95},",",[64,105,106],{"class":99}," 2",[64,108,109],{"class":95},"],",[64,111,112],{"class":95}," [",[64,114,115],{"class":99},"3",[64,117,103],{"class":95},[64,119,120],{"class":99}," 4",[64,122,123],{"class":95},"]]\n",[64,125,127,130,132,135,138,142,145,148],{"class":66,"line":126},4,[64,128,129],{"class":74},"shallow ",[64,131,92],{"class":91},[64,133,134],{"class":74}," copy",[64,136,137],{"class":95},".",[64,139,141],{"class":140},"slqww","copy",[64,143,144],{"class":95},"(",[64,146,147],{"class":140},"original",[64,149,150],{"class":95},")\n",[64,152,154,157,159,161,163,166,168,170],{"class":66,"line":153},5,[64,155,156],{"class":74},"deep ",[64,158,92],{"class":91},[64,160,134],{"class":74},[64,162,137],{"class":95},[64,164,165],{"class":140},"deepcopy",[64,167,144],{"class":95},[64,169,147],{"class":140},[64,171,150],{"class":95},[64,173,175],{"class":66,"line":174},6,[64,176,82],{"emptyLinePlaceholder":81},[64,178,180,182,185,188,191,194,196,199],{"class":66,"line":179},7,[64,181,147],{"class":74},[64,183,184],{"class":95},"[",[64,186,187],{"class":99},"0",[64,189,190],{"class":95},"].",[64,192,193],{"class":140},"append",[64,195,144],{"class":95},[64,197,198],{"class":99},"99",[64,200,150],{"class":95},[64,202,204],{"class":66,"line":203},8,[64,205,82],{"emptyLinePlaceholder":81},[64,207,209,213,215,217,220],{"class":66,"line":208},9,[64,210,212],{"class":211},"sptTA","print",[64,214,144],{"class":95},[64,216,147],{"class":140},[64,218,219],{"class":95},")",[64,221,223],{"class":222},"sutJx","  # [[1, 2, 99], [3, 4]]\n",[64,225,227,229,231,234,236],{"class":66,"line":226},10,[64,228,212],{"class":211},[64,230,144],{"class":95},[64,232,233],{"class":140},"shallow",[64,235,219],{"class":95},[64,237,238],{"class":222},"   # [[1, 2, 99], [3, 4]]\n",[64,240,242,244,246,249,251],{"class":66,"line":241},11,[64,243,212],{"class":211},[64,245,144],{"class":95},[64,247,248],{"class":140},"deep",[64,250,219],{"class":95},[64,252,253],{"class":222},"      # [[1, 2], [3, 4]]\n",[14,255,256,257,260,261,40,263,266],{},"Use ",[61,258,259],{},"copy.copy()"," for a ",[37,262,39],{},[61,264,265],{},"copy.deepcopy()"," when you need fully independent nested objects.",[49,268,270],{"id":269},"what-this-page-helps-you-understand","What this page helps you understand",[21,272,273,276,279,282,285],{},[24,274,275],{},"What a copy means in Python",[24,277,278],{},"How shallow copy works",[24,280,281],{},"How deep copy works",[24,283,284],{},"Why nested objects cause confusion",[24,286,287],{},"When to use each approach",[49,289,291],{"id":290},"what-a-shallow-copy-does","What a shallow copy does",[14,293,294,295,297,298,301],{},"A ",[37,296,39],{}," creates a new outer object, but it does ",[37,299,300],{},"not"," fully copy the nested objects inside it.",[14,303,304],{},"That means:",[21,306,307,310,313],{},[24,308,309],{},"the outer container is new",[24,311,312],{},"inner lists or dictionaries may still be shared",[24,314,315],{},"changing a nested mutable object can affect both versions",[14,317,318],{},"Example:",[54,320,322],{"className":56,"code":321,"language":58,"meta":59,"style":59},"original = [[1, 2], [3, 4]]\ncopied = original.copy()\n\ncopied.append([5, 6])\n\nprint(original)  # [[1, 2], [3, 4]]\nprint(copied)    # [[1, 2], [3, 4], [5, 6]]\n",[61,323,324,350,367,371,394,398,411],{"__ignoreMap":59},[64,325,326,328,330,332,334,336,338,340,342,344,346,348],{"class":66,"line":67},[64,327,88],{"class":74},[64,329,92],{"class":91},[64,331,96],{"class":95},[64,333,100],{"class":99},[64,335,103],{"class":95},[64,337,106],{"class":99},[64,339,109],{"class":95},[64,341,112],{"class":95},[64,343,115],{"class":99},[64,345,103],{"class":95},[64,347,120],{"class":99},[64,349,123],{"class":95},[64,351,352,355,357,360,362,364],{"class":66,"line":78},[64,353,354],{"class":74},"copied ",[64,356,92],{"class":91},[64,358,359],{"class":74}," original",[64,361,137],{"class":95},[64,363,141],{"class":140},[64,365,366],{"class":95},"()\n",[64,368,369],{"class":66,"line":85},[64,370,82],{"emptyLinePlaceholder":81},[64,372,373,376,378,380,383,386,388,391],{"class":66,"line":126},[64,374,375],{"class":74},"copied",[64,377,137],{"class":95},[64,379,193],{"class":140},[64,381,382],{"class":95},"([",[64,384,385],{"class":99},"5",[64,387,103],{"class":95},[64,389,390],{"class":99}," 6",[64,392,393],{"class":95},"])\n",[64,395,396],{"class":66,"line":153},[64,397,82],{"emptyLinePlaceholder":81},[64,399,400,402,404,406,408],{"class":66,"line":174},[64,401,212],{"class":211},[64,403,144],{"class":95},[64,405,147],{"class":140},[64,407,219],{"class":95},[64,409,410],{"class":222},"  # [[1, 2], [3, 4]]\n",[64,412,413,415,417,419,421],{"class":66,"line":179},[64,414,212],{"class":211},[64,416,144],{"class":95},[64,418,375],{"class":140},[64,420,219],{"class":95},[64,422,423],{"class":222},"    # [[1, 2], [3, 4], [5, 6]]\n",[14,425,426,427,429],{},"In this case, appending a new top-level item only changes ",[61,428,375],{},", because the outer list is different.",[14,431,432],{},"But now look at a nested change:",[54,434,436],{"className":56,"code":435,"language":58,"meta":59,"style":59},"original = [[1, 2], [3, 4]]\ncopied = original.copy()\n\ncopied[0].append(99)\n\nprint(original)  # [[1, 2, 99], [3, 4]]\nprint(copied)    # [[1, 2, 99], [3, 4]]\n",[61,437,438,464,478,482,500,504,516],{"__ignoreMap":59},[64,439,440,442,444,446,448,450,452,454,456,458,460,462],{"class":66,"line":67},[64,441,88],{"class":74},[64,443,92],{"class":91},[64,445,96],{"class":95},[64,447,100],{"class":99},[64,449,103],{"class":95},[64,451,106],{"class":99},[64,453,109],{"class":95},[64,455,112],{"class":95},[64,457,115],{"class":99},[64,459,103],{"class":95},[64,461,120],{"class":99},[64,463,123],{"class":95},[64,465,466,468,470,472,474,476],{"class":66,"line":78},[64,467,354],{"class":74},[64,469,92],{"class":91},[64,471,359],{"class":74},[64,473,137],{"class":95},[64,475,141],{"class":140},[64,477,366],{"class":95},[64,479,480],{"class":66,"line":85},[64,481,82],{"emptyLinePlaceholder":81},[64,483,484,486,488,490,492,494,496,498],{"class":66,"line":126},[64,485,375],{"class":74},[64,487,184],{"class":95},[64,489,187],{"class":99},[64,491,190],{"class":95},[64,493,193],{"class":140},[64,495,144],{"class":95},[64,497,198],{"class":99},[64,499,150],{"class":95},[64,501,502],{"class":66,"line":153},[64,503,82],{"emptyLinePlaceholder":81},[64,505,506,508,510,512,514],{"class":66,"line":174},[64,507,212],{"class":211},[64,509,144],{"class":95},[64,511,147],{"class":140},[64,513,219],{"class":95},[64,515,223],{"class":222},[64,517,518,520,522,524,526],{"class":66,"line":179},[64,519,212],{"class":211},[64,521,144],{"class":95},[64,523,375],{"class":140},[64,525,219],{"class":95},[64,527,528],{"class":222},"    # [[1, 2, 99], [3, 4]]\n",[14,530,531,532,40,535,538],{},"Both changed because ",[61,533,534],{},"original[0]",[61,536,537],{},"copied[0]"," point to the same inner list.",[14,540,541,542,137],{},"If you are new to this, it helps to first understand ",[543,544,546],"a",{"href":545},"\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types\u002F","mutability in Python",[49,548,550],{"id":549},"what-a-deep-copy-does","What a deep copy does",[14,552,294,553,555],{},[37,554,43],{}," creates a new outer object and also recursively copies the nested objects inside it.",[14,557,304],{},[21,559,560,562,565],{},[24,561,309],{},[24,563,564],{},"nested mutable objects are also new",[24,566,567],{},"changes inside nested structures do not affect the original",[14,569,318],{},[54,571,573],{"className":56,"code":572,"language":58,"meta":59,"style":59},"import copy\n\noriginal = [[1, 2], [3, 4]]\ncopied = copy.deepcopy(original)\n\ncopied[0].append(99)\n\nprint(original)  # [[1, 2], [3, 4]]\nprint(copied)    # [[1, 2, 99], [3, 4]]\n",[61,574,575,581,585,611,629,633,651,655,667],{"__ignoreMap":59},[64,576,577,579],{"class":66,"line":67},[64,578,71],{"class":70},[64,580,75],{"class":74},[64,582,583],{"class":66,"line":78},[64,584,82],{"emptyLinePlaceholder":81},[64,586,587,589,591,593,595,597,599,601,603,605,607,609],{"class":66,"line":85},[64,588,88],{"class":74},[64,590,92],{"class":91},[64,592,96],{"class":95},[64,594,100],{"class":99},[64,596,103],{"class":95},[64,598,106],{"class":99},[64,600,109],{"class":95},[64,602,112],{"class":95},[64,604,115],{"class":99},[64,606,103],{"class":95},[64,608,120],{"class":99},[64,610,123],{"class":95},[64,612,613,615,617,619,621,623,625,627],{"class":66,"line":126},[64,614,354],{"class":74},[64,616,92],{"class":91},[64,618,134],{"class":74},[64,620,137],{"class":95},[64,622,165],{"class":140},[64,624,144],{"class":95},[64,626,147],{"class":140},[64,628,150],{"class":95},[64,630,631],{"class":66,"line":153},[64,632,82],{"emptyLinePlaceholder":81},[64,634,635,637,639,641,643,645,647,649],{"class":66,"line":174},[64,636,375],{"class":74},[64,638,184],{"class":95},[64,640,187],{"class":99},[64,642,190],{"class":95},[64,644,193],{"class":140},[64,646,144],{"class":95},[64,648,198],{"class":99},[64,650,150],{"class":95},[64,652,653],{"class":66,"line":179},[64,654,82],{"emptyLinePlaceholder":81},[64,656,657,659,661,663,665],{"class":66,"line":203},[64,658,212],{"class":211},[64,660,144],{"class":95},[64,662,147],{"class":140},[64,664,219],{"class":95},[64,666,410],{"class":222},[64,668,669,671,673,675,677],{"class":66,"line":208},[64,670,212],{"class":211},[64,672,144],{"class":95},[64,674,375],{"class":140},[64,676,219],{"class":95},[64,678,528],{"class":222},[14,680,681,682,684,685,137],{},"Here, the inner list was copied too, so changing ",[61,683,537],{}," does not change ",[61,686,534],{},[49,688,690],{"id":689},"why-beginners-get-confused","Why beginners get confused",[14,692,693],{},"This topic is confusing because copying often appears to work at first.",[14,695,696],{},"Common reasons:",[21,698,699,702,705,711],{},[24,700,701],{},"Simple lists of numbers often behave as expected",[24,703,704],{},"Problems usually appear only with nested lists or dictionaries",[24,706,707,708,710],{},"Assignment with ",[61,709,92],{}," does not make a copy",[24,712,713],{},"Some methods make shallow copies, not deep copies",[14,715,716,717,719],{},"For example, this does ",[37,718,300],{}," create a copy:",[54,721,723],{"className":56,"code":722,"language":58,"meta":59,"style":59},"original = [1, 2, 3]\ncopied = original\n\ncopied.append(4)\n\nprint(original)  # [1, 2, 3, 4]\nprint(copied)    # [1, 2, 3, 4]\n",[61,724,725,747,756,760,775,779,792],{"__ignoreMap":59},[64,726,727,729,731,733,735,737,739,741,744],{"class":66,"line":67},[64,728,88],{"class":74},[64,730,92],{"class":91},[64,732,112],{"class":95},[64,734,100],{"class":99},[64,736,103],{"class":95},[64,738,106],{"class":99},[64,740,103],{"class":95},[64,742,743],{"class":99}," 3",[64,745,746],{"class":95},"]\n",[64,748,749,751,753],{"class":66,"line":78},[64,750,354],{"class":74},[64,752,92],{"class":91},[64,754,755],{"class":74}," original\n",[64,757,758],{"class":66,"line":85},[64,759,82],{"emptyLinePlaceholder":81},[64,761,762,764,766,768,770,773],{"class":66,"line":126},[64,763,375],{"class":74},[64,765,137],{"class":95},[64,767,193],{"class":140},[64,769,144],{"class":95},[64,771,772],{"class":99},"4",[64,774,150],{"class":95},[64,776,777],{"class":66,"line":153},[64,778,82],{"emptyLinePlaceholder":81},[64,780,781,783,785,787,789],{"class":66,"line":174},[64,782,212],{"class":211},[64,784,144],{"class":95},[64,786,147],{"class":140},[64,788,219],{"class":95},[64,790,791],{"class":222},"  # [1, 2, 3, 4]\n",[64,793,794,796,798,800,802],{"class":66,"line":179},[64,795,212],{"class":211},[64,797,144],{"class":95},[64,799,375],{"class":140},[64,801,219],{"class":95},[64,803,804],{"class":222},"    # [1, 2, 3, 4]\n",[14,806,807],{},"Both names refer to the same list.",[49,809,811],{"id":810},"assignment-vs-shallow-copy-vs-deep-copy","Assignment vs shallow copy vs deep copy",[14,813,814],{},"These three ideas are different.",[816,817,819],"h3",{"id":818},"assignment","Assignment",[14,821,822],{},"With assignment, both names point to the same object.",[54,824,826],{"className":56,"code":825,"language":58,"meta":59,"style":59},"original = [[1, 2], [3, 4]]\nassigned = original\n\nprint(original is assigned)  # True\n",[61,827,828,854,863,867],{"__ignoreMap":59},[64,829,830,832,834,836,838,840,842,844,846,848,850,852],{"class":66,"line":67},[64,831,88],{"class":74},[64,833,92],{"class":91},[64,835,96],{"class":95},[64,837,100],{"class":99},[64,839,103],{"class":95},[64,841,106],{"class":99},[64,843,109],{"class":95},[64,845,112],{"class":95},[64,847,115],{"class":99},[64,849,103],{"class":95},[64,851,120],{"class":99},[64,853,123],{"class":95},[64,855,856,859,861],{"class":66,"line":78},[64,857,858],{"class":74},"assigned ",[64,860,92],{"class":91},[64,862,755],{"class":74},[64,864,865],{"class":66,"line":85},[64,866,82],{"emptyLinePlaceholder":81},[64,868,869,871,873,875,878,881,883],{"class":66,"line":126},[64,870,212],{"class":211},[64,872,144],{"class":95},[64,874,88],{"class":140},[64,876,877],{"class":70},"is",[64,879,880],{"class":140}," assigned",[64,882,219],{"class":95},[64,884,885],{"class":222},"  # True\n",[14,887,888],{},"Changing one changes the other.",[816,890,892],{"id":891},"shallow-copy","Shallow copy",[14,894,895],{},"With a shallow copy, the outer object is new, but inner objects are shared.",[54,897,899],{"className":56,"code":898,"language":58,"meta":59,"style":59},"original = [[1, 2], [3, 4]]\nshallow = original.copy()\n\nprint(original is shallow)      # False\nprint(original[0] is shallow[0])  # True\n",[61,900,901,927,941,945,963],{"__ignoreMap":59},[64,902,903,905,907,909,911,913,915,917,919,921,923,925],{"class":66,"line":67},[64,904,88],{"class":74},[64,906,92],{"class":91},[64,908,96],{"class":95},[64,910,100],{"class":99},[64,912,103],{"class":95},[64,914,106],{"class":99},[64,916,109],{"class":95},[64,918,112],{"class":95},[64,920,115],{"class":99},[64,922,103],{"class":95},[64,924,120],{"class":99},[64,926,123],{"class":95},[64,928,929,931,933,935,937,939],{"class":66,"line":78},[64,930,129],{"class":74},[64,932,92],{"class":91},[64,934,359],{"class":74},[64,936,137],{"class":95},[64,938,141],{"class":140},[64,940,366],{"class":95},[64,942,943],{"class":66,"line":85},[64,944,82],{"emptyLinePlaceholder":81},[64,946,947,949,951,953,955,958,960],{"class":66,"line":126},[64,948,212],{"class":211},[64,950,144],{"class":95},[64,952,88],{"class":140},[64,954,877],{"class":70},[64,956,957],{"class":140}," shallow",[64,959,219],{"class":95},[64,961,962],{"class":222},"      # False\n",[64,964,965,967,969,971,973,975,978,981,983,985,987,990],{"class":66,"line":153},[64,966,212],{"class":211},[64,968,144],{"class":95},[64,970,147],{"class":140},[64,972,184],{"class":95},[64,974,187],{"class":99},[64,976,977],{"class":95},"]",[64,979,980],{"class":70}," is",[64,982,957],{"class":140},[64,984,184],{"class":95},[64,986,187],{"class":99},[64,988,989],{"class":95},"])",[64,991,885],{"class":222},[816,993,995],{"id":994},"deep-copy","Deep copy",[14,997,998],{},"With a deep copy, both the outer object and inner objects are copied.",[54,1000,1002],{"className":56,"code":1001,"language":58,"meta":59,"style":59},"import copy\n\noriginal = [[1, 2], [3, 4]]\ndeep = copy.deepcopy(original)\n\nprint(original is deep)         # False\nprint(original[0] is deep[0])   # False\n",[61,1003,1004,1010,1014,1040,1058,1062,1080],{"__ignoreMap":59},[64,1005,1006,1008],{"class":66,"line":67},[64,1007,71],{"class":70},[64,1009,75],{"class":74},[64,1011,1012],{"class":66,"line":78},[64,1013,82],{"emptyLinePlaceholder":81},[64,1015,1016,1018,1020,1022,1024,1026,1028,1030,1032,1034,1036,1038],{"class":66,"line":85},[64,1017,88],{"class":74},[64,1019,92],{"class":91},[64,1021,96],{"class":95},[64,1023,100],{"class":99},[64,1025,103],{"class":95},[64,1027,106],{"class":99},[64,1029,109],{"class":95},[64,1031,112],{"class":95},[64,1033,115],{"class":99},[64,1035,103],{"class":95},[64,1037,120],{"class":99},[64,1039,123],{"class":95},[64,1041,1042,1044,1046,1048,1050,1052,1054,1056],{"class":66,"line":126},[64,1043,156],{"class":74},[64,1045,92],{"class":91},[64,1047,134],{"class":74},[64,1049,137],{"class":95},[64,1051,165],{"class":140},[64,1053,144],{"class":95},[64,1055,147],{"class":140},[64,1057,150],{"class":95},[64,1059,1060],{"class":66,"line":153},[64,1061,82],{"emptyLinePlaceholder":81},[64,1063,1064,1066,1068,1070,1072,1075,1077],{"class":66,"line":174},[64,1065,212],{"class":211},[64,1067,144],{"class":95},[64,1069,88],{"class":140},[64,1071,877],{"class":70},[64,1073,1074],{"class":140}," deep",[64,1076,219],{"class":95},[64,1078,1079],{"class":222},"         # False\n",[64,1081,1082,1084,1086,1088,1090,1092,1094,1096,1098,1100,1102,1104],{"class":66,"line":179},[64,1083,212],{"class":211},[64,1085,144],{"class":95},[64,1087,147],{"class":140},[64,1089,184],{"class":95},[64,1091,187],{"class":99},[64,1093,977],{"class":95},[64,1095,980],{"class":70},[64,1097,1074],{"class":140},[64,1099,184],{"class":95},[64,1101,187],{"class":99},[64,1103,989],{"class":95},[64,1105,1106],{"class":222},"   # False\n",[49,1108,1110],{"id":1109},"common-ways-to-make-a-shallow-copy","Common ways to make a shallow copy",[14,1112,1113,1114,1117],{},"These all make ",[37,1115,1116],{},"shallow copies",":",[21,1119,1120,1125,1130,1135,1141],{},[24,1121,1122],{},[61,1123,1124],{},"list.copy()",[24,1126,1127],{},[61,1128,1129],{},"dict.copy()",[24,1131,1132],{},[61,1133,1134],{},"set.copy()",[24,1136,1137,1138],{},"list slicing like ",[61,1139,1140],{},"my_list[:]",[24,1142,1143,1145,1146,1148],{},[61,1144,259],{}," from the ",[61,1147,141],{}," module",[14,1150,1151],{},"Examples:",[54,1153,1155],{"className":56,"code":1154,"language":58,"meta":59,"style":59},"numbers = [1, 2, 3]\n\na = numbers.copy()\nb = numbers[:]\n\nprint(a)  # [1, 2, 3]\nprint(b)  # [1, 2, 3]\n",[61,1156,1157,1178,1182,1198,1210,1214,1227],{"__ignoreMap":59},[64,1158,1159,1162,1164,1166,1168,1170,1172,1174,1176],{"class":66,"line":67},[64,1160,1161],{"class":74},"numbers ",[64,1163,92],{"class":91},[64,1165,112],{"class":95},[64,1167,100],{"class":99},[64,1169,103],{"class":95},[64,1171,106],{"class":99},[64,1173,103],{"class":95},[64,1175,743],{"class":99},[64,1177,746],{"class":95},[64,1179,1180],{"class":66,"line":78},[64,1181,82],{"emptyLinePlaceholder":81},[64,1183,1184,1187,1189,1192,1194,1196],{"class":66,"line":85},[64,1185,1186],{"class":74},"a ",[64,1188,92],{"class":91},[64,1190,1191],{"class":74}," numbers",[64,1193,137],{"class":95},[64,1195,141],{"class":140},[64,1197,366],{"class":95},[64,1199,1200,1203,1205,1207],{"class":66,"line":126},[64,1201,1202],{"class":74},"b ",[64,1204,92],{"class":91},[64,1206,1191],{"class":74},[64,1208,1209],{"class":95},"[:]\n",[64,1211,1212],{"class":66,"line":153},[64,1213,82],{"emptyLinePlaceholder":81},[64,1215,1216,1218,1220,1222,1224],{"class":66,"line":174},[64,1217,212],{"class":211},[64,1219,144],{"class":95},[64,1221,543],{"class":140},[64,1223,219],{"class":95},[64,1225,1226],{"class":222},"  # [1, 2, 3]\n",[64,1228,1229,1231,1233,1236,1238],{"class":66,"line":179},[64,1230,212],{"class":211},[64,1232,144],{"class":95},[64,1234,1235],{"class":140},"b",[64,1237,219],{"class":95},[64,1239,1226],{"class":222},[14,1241,1242,1243,1249,1250,137],{},"For more detail, see the ",[543,1244,1246,1248],{"href":1245},"\u002Freference\u002Fpython-list-copy-method\u002F",[61,1247,1124],{}," method"," and the ",[543,1251,1253,1248],{"href":1252},"\u002Freference\u002Fpython-dictionary-copy-method\u002F",[61,1254,1129],{},[49,1256,1258],{"id":1257},"when-shallow-copy-is-enough","When shallow copy is enough",[14,1260,1261],{},"A shallow copy is often enough when:",[21,1263,1264,1267,1270],{},[24,1265,1266],{},"your data is flat and not nested",[24,1268,1269],{},"you only need a separate outer container",[24,1271,1272],{},"you do not plan to change nested mutable values",[14,1274,1275],{},"Example with a flat list:",[54,1277,1279],{"className":56,"code":1278,"language":58,"meta":59,"style":59},"original = [1, 2, 3]\ncopied = original.copy()\n\ncopied.append(4)\n\nprint(original)  # [1, 2, 3]\nprint(copied)    # [1, 2, 3, 4]\n",[61,1280,1281,1301,1315,1319,1333,1337,1349],{"__ignoreMap":59},[64,1282,1283,1285,1287,1289,1291,1293,1295,1297,1299],{"class":66,"line":67},[64,1284,88],{"class":74},[64,1286,92],{"class":91},[64,1288,112],{"class":95},[64,1290,100],{"class":99},[64,1292,103],{"class":95},[64,1294,106],{"class":99},[64,1296,103],{"class":95},[64,1298,743],{"class":99},[64,1300,746],{"class":95},[64,1302,1303,1305,1307,1309,1311,1313],{"class":66,"line":78},[64,1304,354],{"class":74},[64,1306,92],{"class":91},[64,1308,359],{"class":74},[64,1310,137],{"class":95},[64,1312,141],{"class":140},[64,1314,366],{"class":95},[64,1316,1317],{"class":66,"line":85},[64,1318,82],{"emptyLinePlaceholder":81},[64,1320,1321,1323,1325,1327,1329,1331],{"class":66,"line":126},[64,1322,375],{"class":74},[64,1324,137],{"class":95},[64,1326,193],{"class":140},[64,1328,144],{"class":95},[64,1330,772],{"class":99},[64,1332,150],{"class":95},[64,1334,1335],{"class":66,"line":153},[64,1336,82],{"emptyLinePlaceholder":81},[64,1338,1339,1341,1343,1345,1347],{"class":66,"line":174},[64,1340,212],{"class":211},[64,1342,144],{"class":95},[64,1344,147],{"class":140},[64,1346,219],{"class":95},[64,1348,1226],{"class":222},[64,1350,1351,1353,1355,1357,1359],{"class":66,"line":179},[64,1352,212],{"class":211},[64,1354,144],{"class":95},[64,1356,375],{"class":140},[64,1358,219],{"class":95},[64,1360,804],{"class":222},[14,1362,1363],{},"This works fine because the list contains integers, and integers are immutable.",[14,1365,1366,1367,137],{},"If you just want the common ways to duplicate a list, see ",[543,1368,1370],{"href":1369},"\u002Fhow-to\u002Fhow-to-copy-a-list-in-python\u002F","how to copy a list in Python",[49,1372,1374],{"id":1373},"when-deep-copy-is-safer","When deep copy is safer",[14,1376,1377],{},"A deep copy is safer when:",[21,1379,1380,1383,1386],{},[24,1381,1382],{},"your data contains nested lists, dictionaries, or sets",[24,1384,1385],{},"you need to modify inner values without changing the original",[24,1387,1388],{},"you want a fully independent duplicate",[14,1390,1391],{},"Example with a dictionary containing a list:",[54,1393,1395],{"className":56,"code":1394,"language":58,"meta":59,"style":59},"import copy\n\noriginal = {\"scores\": [10, 20]}\nshallow = original.copy()\ndeep = copy.deepcopy(original)\n\noriginal[\"scores\"].append(30)\n\nprint(original)  # {'scores': [10, 20, 30]}\nprint(shallow)   # {'scores': [10, 20, 30]}\nprint(deep)      # {'scores': [10, 20]}\n",[61,1396,1397,1403,1407,1441,1455,1473,1477,1500,1504,1517,1530],{"__ignoreMap":59},[64,1398,1399,1401],{"class":66,"line":67},[64,1400,71],{"class":70},[64,1402,75],{"class":74},[64,1404,1405],{"class":66,"line":78},[64,1406,82],{"emptyLinePlaceholder":81},[64,1408,1409,1411,1413,1416,1420,1424,1426,1428,1430,1433,1435,1438],{"class":66,"line":85},[64,1410,88],{"class":74},[64,1412,92],{"class":91},[64,1414,1415],{"class":95}," {",[64,1417,1419],{"class":1418},"sjJ54","\"",[64,1421,1423],{"class":1422},"s_sjI","scores",[64,1425,1419],{"class":1418},[64,1427,1117],{"class":95},[64,1429,112],{"class":95},[64,1431,1432],{"class":99},"10",[64,1434,103],{"class":95},[64,1436,1437],{"class":99}," 20",[64,1439,1440],{"class":95},"]}\n",[64,1442,1443,1445,1447,1449,1451,1453],{"class":66,"line":126},[64,1444,129],{"class":74},[64,1446,92],{"class":91},[64,1448,359],{"class":74},[64,1450,137],{"class":95},[64,1452,141],{"class":140},[64,1454,366],{"class":95},[64,1456,1457,1459,1461,1463,1465,1467,1469,1471],{"class":66,"line":153},[64,1458,156],{"class":74},[64,1460,92],{"class":91},[64,1462,134],{"class":74},[64,1464,137],{"class":95},[64,1466,165],{"class":140},[64,1468,144],{"class":95},[64,1470,147],{"class":140},[64,1472,150],{"class":95},[64,1474,1475],{"class":66,"line":174},[64,1476,82],{"emptyLinePlaceholder":81},[64,1478,1479,1481,1483,1485,1487,1489,1491,1493,1495,1498],{"class":66,"line":179},[64,1480,147],{"class":74},[64,1482,184],{"class":95},[64,1484,1419],{"class":1418},[64,1486,1423],{"class":1422},[64,1488,1419],{"class":1418},[64,1490,190],{"class":95},[64,1492,193],{"class":140},[64,1494,144],{"class":95},[64,1496,1497],{"class":99},"30",[64,1499,150],{"class":95},[64,1501,1502],{"class":66,"line":203},[64,1503,82],{"emptyLinePlaceholder":81},[64,1505,1506,1508,1510,1512,1514],{"class":66,"line":208},[64,1507,212],{"class":211},[64,1509,144],{"class":95},[64,1511,147],{"class":140},[64,1513,219],{"class":95},[64,1515,1516],{"class":222},"  # {'scores': [10, 20, 30]}\n",[64,1518,1519,1521,1523,1525,1527],{"class":66,"line":226},[64,1520,212],{"class":211},[64,1522,144],{"class":95},[64,1524,233],{"class":140},[64,1526,219],{"class":95},[64,1528,1529],{"class":222},"   # {'scores': [10, 20, 30]}\n",[64,1531,1532,1534,1536,1538,1540],{"class":66,"line":241},[64,1533,212],{"class":211},[64,1535,144],{"class":95},[64,1537,248],{"class":140},[64,1539,219],{"class":95},[64,1541,1542],{"class":222},"      # {'scores': [10, 20]}\n",[14,1544,1545],{},"The shallow copy still shares the inner list. The deep copy does not.",[49,1547,1549],{"id":1548},"important-limits-and-caution","Important limits and caution",[14,1551,1552],{},"Keep these points in mind:",[21,1554,1555,1558,1561,1564],{},[24,1556,1557],{},"Deep copy can be slower on large objects",[24,1559,1560],{},"Not every object copies cleanly in every situation",[24,1562,1563],{},"Copy only when you really need a separate object",[24,1565,1566],{},"Immutable values like integers and strings do not cause the same shared-mutation problem",[14,1568,1569],{},"For example, strings and integers do not behave like nested lists because they cannot be changed in place.",[54,1571,1573],{"className":56,"code":1572,"language":58,"meta":59,"style":59},"a = 10\nb = a\n\nb = 20\n\nprint(a)  # 10\nprint(b)  # 20\n",[61,1574,1575,1584,1593,1597,1606,1610,1623],{"__ignoreMap":59},[64,1576,1577,1579,1581],{"class":66,"line":67},[64,1578,1186],{"class":74},[64,1580,92],{"class":91},[64,1582,1583],{"class":99}," 10\n",[64,1585,1586,1588,1590],{"class":66,"line":78},[64,1587,1202],{"class":74},[64,1589,92],{"class":91},[64,1591,1592],{"class":74}," a\n",[64,1594,1595],{"class":66,"line":85},[64,1596,82],{"emptyLinePlaceholder":81},[64,1598,1599,1601,1603],{"class":66,"line":126},[64,1600,1202],{"class":74},[64,1602,92],{"class":91},[64,1604,1605],{"class":99}," 20\n",[64,1607,1608],{"class":66,"line":153},[64,1609,82],{"emptyLinePlaceholder":81},[64,1611,1612,1614,1616,1618,1620],{"class":66,"line":174},[64,1613,212],{"class":211},[64,1615,144],{"class":95},[64,1617,543],{"class":140},[64,1619,219],{"class":95},[64,1621,1622],{"class":222},"  # 10\n",[64,1624,1625,1627,1629,1631,1633],{"class":66,"line":179},[64,1626,212],{"class":211},[64,1628,144],{"class":95},[64,1630,1235],{"class":140},[64,1632,219],{"class":95},[64,1634,1635],{"class":222},"  # 20\n",[14,1637,1638,1639,1642],{},"This is one reason ",[543,1640,1641],{"href":545},"mutable vs immutable types"," matters when learning about copying.",[49,1644,1646],{"id":1645},"common-mistakes","Common mistakes",[14,1648,1649],{},"These mistakes cause most copy-related bugs:",[21,1651,1652,1658,1663,1668,1671],{},[24,1653,1654,1655,1657],{},"Using ",[61,1656,92],{}," and expecting a real copy",[24,1659,1654,1660,1662],{},[61,1661,1124],{}," on a nested list and expecting full independence",[24,1664,1654,1665,1667],{},[61,1666,1129],{}," on a dictionary that contains lists or dictionaries",[24,1669,1670],{},"Changing an inner list after making a shallow copy",[24,1672,1673],{},"Not realizing the difference between mutable and immutable values",[14,1675,1676],{},"If something seems wrong, these checks can help:",[54,1678,1680],{"className":56,"code":1679,"language":58,"meta":59,"style":59},"print(original is copied)\nprint(original == copied)\nprint(id(original), id(copied))\nprint(id(original[0]), id(copied[0]))\nprint(type(original))\n",[61,1681,1682,1697,1712,1738,1770],{"__ignoreMap":59},[64,1683,1684,1686,1688,1690,1692,1695],{"class":66,"line":67},[64,1685,212],{"class":211},[64,1687,144],{"class":95},[64,1689,88],{"class":140},[64,1691,877],{"class":70},[64,1693,1694],{"class":140}," copied",[64,1696,150],{"class":95},[64,1698,1699,1701,1703,1705,1708,1710],{"class":66,"line":78},[64,1700,212],{"class":211},[64,1702,144],{"class":95},[64,1704,88],{"class":140},[64,1706,1707],{"class":91},"==",[64,1709,1694],{"class":140},[64,1711,150],{"class":95},[64,1713,1714,1716,1718,1721,1723,1725,1728,1731,1733,1735],{"class":66,"line":85},[64,1715,212],{"class":211},[64,1717,144],{"class":95},[64,1719,1720],{"class":211},"id",[64,1722,144],{"class":95},[64,1724,147],{"class":140},[64,1726,1727],{"class":95},"),",[64,1729,1730],{"class":211}," id",[64,1732,144],{"class":95},[64,1734,375],{"class":140},[64,1736,1737],{"class":95},"))\n",[64,1739,1740,1742,1744,1746,1748,1750,1752,1754,1757,1759,1761,1763,1765,1767],{"class":66,"line":126},[64,1741,212],{"class":211},[64,1743,144],{"class":95},[64,1745,1720],{"class":211},[64,1747,144],{"class":95},[64,1749,147],{"class":140},[64,1751,184],{"class":95},[64,1753,187],{"class":99},[64,1755,1756],{"class":95},"]),",[64,1758,1730],{"class":211},[64,1760,144],{"class":95},[64,1762,375],{"class":140},[64,1764,184],{"class":95},[64,1766,187],{"class":99},[64,1768,1769],{"class":95},"]))\n",[64,1771,1772,1774,1776,1780,1782,1784],{"class":66,"line":153},[64,1773,212],{"class":211},[64,1775,144],{"class":95},[64,1777,1779],{"class":1778},"sZMiF","type",[64,1781,144],{"class":95},[64,1783,147],{"class":140},[64,1785,1737],{"class":95},[14,1787,1788],{},"What they tell you:",[21,1790,1791,1796,1801,1807,1817],{},[24,1792,1793,1795],{},[61,1794,877],{}," checks whether two names refer to the same object",[24,1797,1798,1800],{},[61,1799,1707],{}," checks whether values are equal",[24,1802,1803,1806],{},[61,1804,1805],{},"id()"," shows the identity of an object",[24,1808,1809,1810,40,1813,1816],{},"comparing ",[61,1811,1812],{},"id(original[0])",[61,1814,1815],{},"id(copied[0])"," helps detect shared inner objects",[24,1818,1819,1822],{},[61,1820,1821],{},"type()"," confirms what kind of object you are copying",[49,1824,1826],{"id":1825},"faq","FAQ",[816,1828,1830],{"id":1829},"does-create-a-copy-in-python","Does = create a copy in Python?",[14,1832,1833],{},"No. It creates another reference to the same object.",[816,1835,1837],{"id":1836},"is-listcopy-a-deep-copy","Is list.copy() a deep copy?",[14,1839,1840],{},"No. It is a shallow copy.",[816,1842,1844],{"id":1843},"when-do-i-need-deepcopy","When do I need deepcopy()?",[14,1846,1847],{},"Use it when your object contains nested mutable values and you want full independence.",[816,1849,1851],{"id":1850},"do-strings-and-integers-need-deep-copy","Do strings and integers need deep copy?",[14,1853,1854],{},"Usually no. They are immutable, so changing one variable does not change the original value.",[816,1856,1858],{"id":1857},"why-did-changing-one-list-change-the-other","Why did changing one list change the other?",[14,1860,1861],{},"They likely share the same inner object because of assignment or a shallow copy.",[49,1863,1865],{"id":1864},"see-also","See also",[21,1867,1868,1873,1880,1886,1891],{},[24,1869,1870],{},[543,1871,1872],{"href":545},"Mutability in Python explained: mutable vs immutable types",[24,1874,1875],{},[543,1876,1877,1878,1248],{"href":1245},"Python ",[61,1879,1124],{},[24,1881,1882],{},[543,1883,1877,1884,1248],{"href":1252},[61,1885,1129],{},[24,1887,1888],{},[543,1889,1890],{"href":1369},"How to copy a list in Python",[24,1892,1893],{},[543,1894,1896],{"href":1895},"\u002Fglossary\u002Fwhat-is-a-list-in-python\u002F","What is a list in Python?",[1898,1899,1900],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":59,"searchDepth":78,"depth":78,"links":1902},[1903,1904,1905,1906,1907,1908,1913,1914,1915,1916,1917,1918,1925],{"id":51,"depth":78,"text":52},{"id":269,"depth":78,"text":270},{"id":290,"depth":78,"text":291},{"id":549,"depth":78,"text":550},{"id":689,"depth":78,"text":690},{"id":810,"depth":78,"text":811,"children":1909},[1910,1911,1912],{"id":818,"depth":85,"text":819},{"id":891,"depth":85,"text":892},{"id":994,"depth":85,"text":995},{"id":1109,"depth":78,"text":1110},{"id":1257,"depth":78,"text":1258},{"id":1373,"depth":78,"text":1374},{"id":1548,"depth":78,"text":1549},{"id":1645,"depth":78,"text":1646},{"id":1825,"depth":78,"text":1826,"children":1919},[1920,1921,1922,1923,1924],{"id":1829,"depth":85,"text":1830},{"id":1836,"depth":85,"text":1837},{"id":1843,"depth":85,"text":1844},{"id":1850,"depth":85,"text":1851},{"id":1857,"depth":85,"text":1858},{"id":1864,"depth":78,"text":1865},"Master python shallow copy vs deep copy explained in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-shallow-copy-vs-deep-copy-explained",{"title":5,"description":1926},"reference\u002Fpython-shallow-copy-vs-deep-copy-explained","4P8qWKs1knuRzO-mq5pz1MvM10FXTsG04QbHQz5pkwk",1777585491583]