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