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