[{"data":1,"prerenderedAt":2325},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-sorted-vs-list.sort-explained":3},{"id":4,"title":5,"body":6,"description":2318,"extension":2319,"meta":2320,"navigation":133,"path":2321,"seo":2322,"stem":2323,"__hash__":2324},"content\u002Freference\u002Fpython-sorted-vs-list.sort-explained.md","Python sorted() vs list.sort() Explained",{"type":7,"value":8,"toc":2263},"minimark",[9,23,27,39,47,50,69,79,84,178,186,190,212,219,224,227,244,247,264,272,367,372,424,430,440,445,451,453,466,472,526,530,552,559,568,572,575,596,600,720,724,797,800,812,818,823,837,841,922,926,972,980,988,993,997,1011,1015,1086,1090,1123,1126,1133,1144,1152,1166,1169,1173,1260,1264,1301,1305,1417,1421,1482,1489,1496,1500,1503,1510,1513,1580,1584,1610,1613,1634,1641,1694,1698,1719,1725,1728,1787,1794,1824,1830,1835,1845,1852,1912,1916,1965,1968,1972,1975,2003,2006,2101,2104,2126,2130,2133,2151,2157,2161,2171,2176,2185,2188,2195,2198,2205,2215,2224,2229,2233,2259],[10,11,13,14,18,19,22],"h1",{"id":12},"python-sorted-vs-listsort-explained","Python ",[15,16,17],"code",{},"sorted()"," vs ",[15,20,21],{},"list.sort()"," Explained",[24,25,26],"p",{},"Python gives you two common ways to sort values:",[28,29,30,35],"ul",{},[31,32,33],"li",{},[15,34,17],{},[31,36,37],{},[15,38,21],{},[24,40,41,42,46],{},"They look similar, but they do ",[43,44,45],"strong",{},"not"," behave the same way.",[24,48,49],{},"The main difference is simple:",[28,51,52,61],{},[31,53,54,55,57,58],{},"Use ",[15,56,17],{}," when you want a ",[43,59,60],{},"new sorted result",[31,62,54,63,65,66],{},[15,64,21],{}," when you want to ",[43,67,68],{},"change the original list",[24,70,71,72,74,75,78],{},"This matters a lot for beginners, especially when ",[15,73,21],{}," returns ",[15,76,77],{},"None"," and causes confusion.",[80,81,83],"h2",{"id":82},"quick-answer","Quick answer",[85,86,91],"pre",{"className":87,"code":88,"language":89,"meta":90,"style":90},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","numbers = [3, 1, 2]\n\nnew_list = sorted(numbers)   # returns a new sorted list\nnumbers.sort()              # sorts the original list in place\n","python","",[15,92,93,128,135,161],{"__ignoreMap":90},[94,95,98,102,106,110,114,117,120,122,125],"span",{"class":96,"line":97},"line",1,[94,99,101],{"class":100},"su5hD","numbers ",[94,103,105],{"class":104},"smGrS","=",[94,107,109],{"class":108},"sP7_E"," [",[94,111,113],{"class":112},"srdBf","3",[94,115,116],{"class":108},",",[94,118,119],{"class":112}," 1",[94,121,116],{"class":108},[94,123,124],{"class":112}," 2",[94,126,127],{"class":108},"]\n",[94,129,131],{"class":96,"line":130},2,[94,132,134],{"emptyLinePlaceholder":133},true,"\n",[94,136,138,141,143,147,150,154,157],{"class":96,"line":137},3,[94,139,140],{"class":100},"new_list ",[94,142,105],{"class":104},[94,144,146],{"class":145},"sptTA"," sorted",[94,148,149],{"class":108},"(",[94,151,153],{"class":152},"slqww","numbers",[94,155,156],{"class":108},")",[94,158,160],{"class":159},"sutJx","   # returns a new sorted list\n",[94,162,164,166,169,172,175],{"class":96,"line":163},4,[94,165,153],{"class":100},[94,167,168],{"class":108},".",[94,170,171],{"class":152},"sort",[94,173,174],{"class":108},"()",[94,176,177],{"class":159},"              # sorts the original list in place\n",[24,179,54,180,182,183,185],{},[15,181,17],{}," when you want a new result. Use ",[15,184,21],{}," when you want to change the existing list.",[80,187,189],{"id":188},"what-this-page-helps-you-decide","What this page helps you decide",[28,191,192,197,202,209],{},[31,193,54,194,196],{},[15,195,17],{}," to get a new sorted list",[31,198,54,199,201],{},[15,200,21],{}," to sort an existing list in place",[31,203,204,205,74,207],{},"Understand why ",[15,206,21],{},[15,208,77],{},[31,210,211],{},"See when each option is better for beginner code",[80,213,215,216,218],{"id":214},"what-sorted-does","What ",[15,217,17],{}," does",[24,220,221,223],{},[15,222,17],{}," is a built-in Python function.",[24,225,226],{},"It:",[28,228,229,235,241],{},[31,230,231,232],{},"returns a ",[43,233,234],{},"new list",[31,236,237,238,240],{},"does ",[43,239,45],{}," change the original data",[31,242,243],{},"works with many iterable objects, not just lists",[24,245,246],{},"That includes:",[28,248,249,252,255,258,261],{},[31,250,251],{},"lists",[31,253,254],{},"tuples",[31,256,257],{},"strings",[31,259,260],{},"sets",[31,262,263],{},"other iterable objects",[265,266,268,269,271],"h3",{"id":267},"example-sorted-keeps-the-original-list-unchanged","Example: ",[15,270,17],{}," keeps the original list unchanged",[85,273,275],{"className":87,"code":274,"language":89,"meta":90,"style":90},"numbers = [3, 1, 2]\n\nresult = sorted(numbers)\n\nprint(\"original:\", numbers)\nprint(\"sorted result:\", result)\n",[15,276,277,297,301,317,321,346],{"__ignoreMap":90},[94,278,279,281,283,285,287,289,291,293,295],{"class":96,"line":97},[94,280,101],{"class":100},[94,282,105],{"class":104},[94,284,109],{"class":108},[94,286,113],{"class":112},[94,288,116],{"class":108},[94,290,119],{"class":112},[94,292,116],{"class":108},[94,294,124],{"class":112},[94,296,127],{"class":108},[94,298,299],{"class":96,"line":130},[94,300,134],{"emptyLinePlaceholder":133},[94,302,303,306,308,310,312,314],{"class":96,"line":137},[94,304,305],{"class":100},"result ",[94,307,105],{"class":104},[94,309,146],{"class":145},[94,311,149],{"class":108},[94,313,153],{"class":152},[94,315,316],{"class":108},")\n",[94,318,319],{"class":96,"line":163},[94,320,134],{"emptyLinePlaceholder":133},[94,322,324,327,329,333,337,339,341,344],{"class":96,"line":323},5,[94,325,326],{"class":145},"print",[94,328,149],{"class":108},[94,330,332],{"class":331},"sjJ54","\"",[94,334,336],{"class":335},"s_sjI","original:",[94,338,332],{"class":331},[94,340,116],{"class":108},[94,342,343],{"class":152}," numbers",[94,345,316],{"class":108},[94,347,349,351,353,355,358,360,362,365],{"class":96,"line":348},6,[94,350,326],{"class":145},[94,352,149],{"class":108},[94,354,332],{"class":331},[94,356,357],{"class":335},"sorted result:",[94,359,332],{"class":331},[94,361,116],{"class":108},[94,363,364],{"class":152}," result",[94,366,316],{"class":108},[24,368,369],{},[43,370,371],{},"Output:",[85,373,375],{"className":87,"code":374,"language":89,"meta":90,"style":90},"original: [3, 1, 2]\nsorted result: [1, 2, 3]\n",[15,376,377,399],{"__ignoreMap":90},[94,378,379,382,385,387,389,391,393,395,397],{"class":96,"line":97},[94,380,381],{"class":100},"original",[94,383,384],{"class":108},":",[94,386,109],{"class":108},[94,388,113],{"class":112},[94,390,116],{"class":108},[94,392,119],{"class":112},[94,394,116],{"class":108},[94,396,124],{"class":112},[94,398,127],{"class":108},[94,400,401,404,406,408,410,413,415,417,419,422],{"class":96,"line":130},[94,402,403],{"class":145},"sorted",[94,405,364],{"class":100},[94,407,384],{"class":108},[94,409,109],{"class":108},[94,411,412],{"class":112},"1",[94,414,116],{"class":108},[94,416,124],{"class":112},[94,418,116],{"class":108},[94,420,421],{"class":112}," 3",[94,423,127],{"class":108},[24,425,426,427,429],{},"The important part is that ",[15,428,153],{}," stays the same.",[24,431,432,433,168],{},"If you want a full function-focused explanation, see ",[434,435,13,437,439],"a",{"href":436},"\u002Freference\u002Fpython-sorted-function-explained\u002F",[15,438,17],{}," function explained",[80,441,215,443,218],{"id":442},"what-listsort-does",[15,444,21],{},[24,446,447,450],{},[15,448,449],{},"sort()"," is a method that belongs to lists.",[24,452,226],{},[28,454,455,458,461],{},[31,456,457],{},"changes the original list directly",[31,459,460],{},"only works on lists",[31,462,463,464],{},"returns ",[15,465,77],{},[265,467,268,469,471],{"id":468},"example-listsort-changes-the-original-list",[15,470,21],{}," changes the original list",[85,473,475],{"className":87,"code":474,"language":89,"meta":90,"style":90},"numbers = [3, 1, 2]\n\nnumbers.sort()\n\nprint(numbers)\n",[15,476,477,497,501,512,516],{"__ignoreMap":90},[94,478,479,481,483,485,487,489,491,493,495],{"class":96,"line":97},[94,480,101],{"class":100},[94,482,105],{"class":104},[94,484,109],{"class":108},[94,486,113],{"class":112},[94,488,116],{"class":108},[94,490,119],{"class":112},[94,492,116],{"class":108},[94,494,124],{"class":112},[94,496,127],{"class":108},[94,498,499],{"class":96,"line":130},[94,500,134],{"emptyLinePlaceholder":133},[94,502,503,505,507,509],{"class":96,"line":137},[94,504,153],{"class":100},[94,506,168],{"class":108},[94,508,171],{"class":152},[94,510,511],{"class":108},"()\n",[94,513,514],{"class":96,"line":163},[94,515,134],{"emptyLinePlaceholder":133},[94,517,518,520,522,524],{"class":96,"line":323},[94,519,326],{"class":145},[94,521,149],{"class":108},[94,523,153],{"class":152},[94,525,316],{"class":108},[24,527,528],{},[43,529,371],{},[85,531,533],{"className":87,"code":532,"language":89,"meta":90,"style":90},"[1, 2, 3]\n",[15,534,535],{"__ignoreMap":90},[94,536,537,540,542,544,546,548,550],{"class":96,"line":97},[94,538,539],{"class":108},"[",[94,541,412],{"class":112},[94,543,116],{"class":108},[94,545,124],{"class":112},[94,547,116],{"class":108},[94,549,421],{"class":112},[94,551,127],{"class":108},[24,553,554,555,558],{},"After calling ",[15,556,557],{},"numbers.sort()",", the list itself is changed.",[24,560,561,562,168],{},"For a method-specific reference page, see ",[434,563,13,565,567],{"href":564},"\u002Freference\u002Fpython-list-sort-method\u002F",[15,566,21],{}," method",[80,569,571],{"id":570},"main-difference-at-a-glance","Main difference at a glance",[24,573,574],{},"Here is the core difference:",[28,576,577,586],{},[31,578,579,582,583],{},[15,580,581],{},"sorted(iterable)"," → returns a ",[43,584,585],{},"new sorted list",[31,587,588,591,592,595],{},[15,589,590],{},"my_list.sort()"," → changes ",[15,593,594],{},"my_list"," directly",[265,597,599],{"id":598},"side-by-side-example","Side-by-side example",[85,601,603],{"className":87,"code":602,"language":89,"meta":90,"style":90},"numbers = [5, 2, 4]\n\na = sorted(numbers)\nprint(\"numbers after sorted():\", numbers)\nprint(\"a:\", a)\n\nnumbers.sort()\nprint(\"numbers after sort():\", numbers)\n",[15,604,605,627,631,646,665,685,689,700],{"__ignoreMap":90},[94,606,607,609,611,613,616,618,620,622,625],{"class":96,"line":97},[94,608,101],{"class":100},[94,610,105],{"class":104},[94,612,109],{"class":108},[94,614,615],{"class":112},"5",[94,617,116],{"class":108},[94,619,124],{"class":112},[94,621,116],{"class":108},[94,623,624],{"class":112}," 4",[94,626,127],{"class":108},[94,628,629],{"class":96,"line":130},[94,630,134],{"emptyLinePlaceholder":133},[94,632,633,636,638,640,642,644],{"class":96,"line":137},[94,634,635],{"class":100},"a ",[94,637,105],{"class":104},[94,639,146],{"class":145},[94,641,149],{"class":108},[94,643,153],{"class":152},[94,645,316],{"class":108},[94,647,648,650,652,654,657,659,661,663],{"class":96,"line":163},[94,649,326],{"class":145},[94,651,149],{"class":108},[94,653,332],{"class":331},[94,655,656],{"class":335},"numbers after sorted():",[94,658,332],{"class":331},[94,660,116],{"class":108},[94,662,343],{"class":152},[94,664,316],{"class":108},[94,666,667,669,671,673,676,678,680,683],{"class":96,"line":323},[94,668,326],{"class":145},[94,670,149],{"class":108},[94,672,332],{"class":331},[94,674,675],{"class":335},"a:",[94,677,332],{"class":331},[94,679,116],{"class":108},[94,681,682],{"class":152}," a",[94,684,316],{"class":108},[94,686,687],{"class":96,"line":348},[94,688,134],{"emptyLinePlaceholder":133},[94,690,692,694,696,698],{"class":96,"line":691},7,[94,693,153],{"class":100},[94,695,168],{"class":108},[94,697,171],{"class":152},[94,699,511],{"class":108},[94,701,703,705,707,709,712,714,716,718],{"class":96,"line":702},8,[94,704,326],{"class":145},[94,706,149],{"class":108},[94,708,332],{"class":331},[94,710,711],{"class":335},"numbers after sort():",[94,713,332],{"class":331},[94,715,116],{"class":108},[94,717,343],{"class":152},[94,719,316],{"class":108},[24,721,722],{},[43,723,371],{},[85,725,727],{"className":87,"code":726,"language":89,"meta":90,"style":90},"numbers after sorted(): [5, 2, 4]\na: [2, 4, 5]\nnumbers after sort(): [2, 4, 5]\n",[15,728,729,753,775],{"__ignoreMap":90},[94,730,731,734,736,739,741,743,745,747,749,751],{"class":96,"line":97},[94,732,733],{"class":100},"numbers after ",[94,735,403],{"class":145},[94,737,738],{"class":108},"():",[94,740,109],{"class":108},[94,742,615],{"class":112},[94,744,116],{"class":108},[94,746,124],{"class":112},[94,748,116],{"class":108},[94,750,624],{"class":112},[94,752,127],{"class":108},[94,754,755,757,759,761,764,766,768,770,773],{"class":96,"line":130},[94,756,434],{"class":100},[94,758,384],{"class":108},[94,760,109],{"class":108},[94,762,763],{"class":112},"2",[94,765,116],{"class":108},[94,767,624],{"class":112},[94,769,116],{"class":108},[94,771,772],{"class":112}," 5",[94,774,127],{"class":108},[94,776,777,779,781,783,785,787,789,791,793,795],{"class":96,"line":137},[94,778,733],{"class":100},[94,780,171],{"class":152},[94,782,738],{"class":108},[94,784,109],{"class":108},[94,786,763],{"class":112},[94,788,116],{"class":108},[94,790,624],{"class":112},[94,792,116],{"class":108},[94,794,772],{"class":112},[94,796,127],{"class":108},[24,798,799],{},"So:",[28,801,802,807],{},[31,803,804,806],{},[15,805,17],{}," keeps the original data unchanged",[31,808,809,811],{},[15,810,449],{}," is useful when you do not need the original order anymore",[80,813,815,816],{"id":814},"when-to-use-sorted","When to use ",[15,817,17],{},[24,819,54,820,822],{},[15,821,17],{}," when:",[28,824,825,828,831,834],{},[31,826,827],{},"you want to keep the original list unchanged",[31,829,830],{},"your data is not a list",[31,832,833],{},"you want to store the result in a new variable",[31,835,836],{},"you want clearer code that is easy to read",[265,838,840],{"id":839},"example-sorting-a-tuple","Example: sorting a tuple",[85,842,844],{"className":87,"code":843,"language":89,"meta":90,"style":90},"numbers = (4, 1, 3, 2)\n\nresult = sorted(numbers)\n\nprint(result)\nprint(type(result))\n",[15,845,846,872,876,890,894,905],{"__ignoreMap":90},[94,847,848,850,852,855,858,860,862,864,866,868,870],{"class":96,"line":97},[94,849,101],{"class":100},[94,851,105],{"class":104},[94,853,854],{"class":108}," (",[94,856,857],{"class":112},"4",[94,859,116],{"class":108},[94,861,119],{"class":112},[94,863,116],{"class":108},[94,865,421],{"class":112},[94,867,116],{"class":108},[94,869,124],{"class":112},[94,871,316],{"class":108},[94,873,874],{"class":96,"line":130},[94,875,134],{"emptyLinePlaceholder":133},[94,877,878,880,882,884,886,888],{"class":96,"line":137},[94,879,305],{"class":100},[94,881,105],{"class":104},[94,883,146],{"class":145},[94,885,149],{"class":108},[94,887,153],{"class":152},[94,889,316],{"class":108},[94,891,892],{"class":96,"line":163},[94,893,134],{"emptyLinePlaceholder":133},[94,895,896,898,900,903],{"class":96,"line":323},[94,897,326],{"class":145},[94,899,149],{"class":108},[94,901,902],{"class":152},"result",[94,904,316],{"class":108},[94,906,907,909,911,915,917,919],{"class":96,"line":348},[94,908,326],{"class":145},[94,910,149],{"class":108},[94,912,914],{"class":913},"sZMiF","type",[94,916,149],{"class":108},[94,918,902],{"class":152},[94,920,921],{"class":108},"))\n",[24,923,924],{},[43,925,371],{},[85,927,929],{"className":87,"code":928,"language":89,"meta":90,"style":90},"[1, 2, 3, 4]\n\u003Cclass 'list'>\n",[15,930,931,951],{"__ignoreMap":90},[94,932,933,935,937,939,941,943,945,947,949],{"class":96,"line":97},[94,934,539],{"class":108},[94,936,412],{"class":112},[94,938,116],{"class":108},[94,940,124],{"class":112},[94,942,116],{"class":108},[94,944,421],{"class":112},[94,946,116],{"class":108},[94,948,624],{"class":112},[94,950,127],{"class":108},[94,952,953,956,960,963,966,969],{"class":96,"line":130},[94,954,955],{"class":104},"\u003C",[94,957,959],{"class":958},"sbsja","class",[94,961,962],{"class":331}," '",[94,964,965],{"class":335},"list",[94,967,968],{"class":331},"'",[94,970,971],{"class":104},">\n",[24,973,974,975,977,978,168],{},"Notice that ",[15,976,17],{}," can sort a tuple, but the result is a ",[43,979,965],{},[24,981,982,983,987],{},"If you are still learning list behavior, ",[434,984,986],{"href":985},"\u002Flearn\u002Fpython-lists-explained-beginner-guide\u002F","Python lists explained for beginners"," is a helpful next step.",[80,989,815,991],{"id":990},"when-to-use-listsort",[15,992,21],{},[24,994,54,995,822],{},[15,996,21],{},[28,998,999,1002,1005,1008],{},[31,1000,1001],{},"you already have a list",[31,1003,1004],{},"you want to update that list directly",[31,1006,1007],{},"you do not need the original order anymore",[31,1009,1010],{},"you want a simple in-place change",[265,1012,1014],{"id":1013},"example-sorting-one-list-directly","Example: sorting one list directly",[85,1016,1018],{"className":87,"code":1017,"language":89,"meta":90,"style":90},"names = [\"Charlie\", \"Alice\", \"Bob\"]\n\nnames.sort()\n\nprint(names)\n",[15,1019,1020,1057,1061,1072,1076],{"__ignoreMap":90},[94,1021,1022,1025,1027,1029,1031,1034,1036,1038,1041,1044,1046,1048,1050,1053,1055],{"class":96,"line":97},[94,1023,1024],{"class":100},"names ",[94,1026,105],{"class":104},[94,1028,109],{"class":108},[94,1030,332],{"class":331},[94,1032,1033],{"class":335},"Charlie",[94,1035,332],{"class":331},[94,1037,116],{"class":108},[94,1039,1040],{"class":331}," \"",[94,1042,1043],{"class":335},"Alice",[94,1045,332],{"class":331},[94,1047,116],{"class":108},[94,1049,1040],{"class":331},[94,1051,1052],{"class":335},"Bob",[94,1054,332],{"class":331},[94,1056,127],{"class":108},[94,1058,1059],{"class":96,"line":130},[94,1060,134],{"emptyLinePlaceholder":133},[94,1062,1063,1066,1068,1070],{"class":96,"line":137},[94,1064,1065],{"class":100},"names",[94,1067,168],{"class":108},[94,1069,171],{"class":152},[94,1071,511],{"class":108},[94,1073,1074],{"class":96,"line":163},[94,1075,134],{"emptyLinePlaceholder":133},[94,1077,1078,1080,1082,1084],{"class":96,"line":323},[94,1079,326],{"class":145},[94,1081,149],{"class":108},[94,1083,1065],{"class":152},[94,1085,316],{"class":108},[24,1087,1088],{},[43,1089,371],{},[85,1091,1093],{"className":87,"code":1092,"language":89,"meta":90,"style":90},"['Alice', 'Bob', 'Charlie']\n",[15,1094,1095],{"__ignoreMap":90},[94,1096,1097,1099,1101,1103,1105,1107,1109,1111,1113,1115,1117,1119,1121],{"class":96,"line":97},[94,1098,539],{"class":108},[94,1100,968],{"class":331},[94,1102,1043],{"class":335},[94,1104,968],{"class":331},[94,1106,116],{"class":108},[94,1108,962],{"class":331},[94,1110,1052],{"class":335},[94,1112,968],{"class":331},[94,1114,116],{"class":108},[94,1116,962],{"class":331},[94,1118,1033],{"class":335},[94,1120,968],{"class":331},[94,1122,127],{"class":108},[24,1124,1125],{},"This is a good choice when you only need the list in sorted form and do not care about keeping the old order.",[24,1127,1128,1129,168],{},"If your goal is the task itself, see ",[434,1130,1132],{"href":1131},"\u002Fhow-to\u002Fhow-to-sort-a-list-in-python\u002F","How to sort a list in Python",[80,1134,1136,1139,1140,1143],{"id":1135},"reverse-and-key-work-with-both",[15,1137,1138],{},"reverse"," and ",[15,1141,1142],{},"key"," work with both",[24,1145,1146,1147,1139,1149,1151],{},"Both ",[15,1148,17],{},[15,1150,21],{}," support:",[28,1153,1154,1160],{},[31,1155,1156,1159],{},[15,1157,1158],{},"reverse=True"," for descending order",[31,1161,1162,1165],{},[15,1163,1164],{},"key=..."," for custom sorting rules",[24,1167,1168],{},"The sorting behavior is very similar. The main difference is still whether you get a new list or change the original one.",[265,1170,1172],{"id":1171},"example-descending-order","Example: descending order",[85,1174,1176],{"className":87,"code":1175,"language":89,"meta":90,"style":90},"numbers = [3, 1, 2]\n\nprint(sorted(numbers, reverse=True))\n\nnumbers.sort(reverse=True)\nprint(numbers)\n",[15,1177,1178,1198,1202,1228,1232,1250],{"__ignoreMap":90},[94,1179,1180,1182,1184,1186,1188,1190,1192,1194,1196],{"class":96,"line":97},[94,1181,101],{"class":100},[94,1183,105],{"class":104},[94,1185,109],{"class":108},[94,1187,113],{"class":112},[94,1189,116],{"class":108},[94,1191,119],{"class":112},[94,1193,116],{"class":108},[94,1195,124],{"class":112},[94,1197,127],{"class":108},[94,1199,1200],{"class":96,"line":130},[94,1201,134],{"emptyLinePlaceholder":133},[94,1203,1204,1206,1208,1210,1212,1214,1216,1220,1222,1226],{"class":96,"line":137},[94,1205,326],{"class":145},[94,1207,149],{"class":108},[94,1209,403],{"class":145},[94,1211,149],{"class":108},[94,1213,153],{"class":152},[94,1215,116],{"class":108},[94,1217,1219],{"class":1218},"s99_P"," reverse",[94,1221,105],{"class":104},[94,1223,1225],{"class":1224},"s39Yj","True",[94,1227,921],{"class":108},[94,1229,1230],{"class":96,"line":163},[94,1231,134],{"emptyLinePlaceholder":133},[94,1233,1234,1236,1238,1240,1242,1244,1246,1248],{"class":96,"line":323},[94,1235,153],{"class":100},[94,1237,168],{"class":108},[94,1239,171],{"class":152},[94,1241,149],{"class":108},[94,1243,1138],{"class":1218},[94,1245,105],{"class":104},[94,1247,1225],{"class":1224},[94,1249,316],{"class":108},[94,1251,1252,1254,1256,1258],{"class":96,"line":348},[94,1253,326],{"class":145},[94,1255,149],{"class":108},[94,1257,153],{"class":152},[94,1259,316],{"class":108},[24,1261,1262],{},[43,1263,371],{},[85,1265,1267],{"className":87,"code":1266,"language":89,"meta":90,"style":90},"[3, 2, 1]\n[3, 2, 1]\n",[15,1268,1269,1285],{"__ignoreMap":90},[94,1270,1271,1273,1275,1277,1279,1281,1283],{"class":96,"line":97},[94,1272,539],{"class":108},[94,1274,113],{"class":112},[94,1276,116],{"class":108},[94,1278,124],{"class":112},[94,1280,116],{"class":108},[94,1282,119],{"class":112},[94,1284,127],{"class":108},[94,1286,1287,1289,1291,1293,1295,1297,1299],{"class":96,"line":130},[94,1288,539],{"class":108},[94,1290,113],{"class":112},[94,1292,116],{"class":108},[94,1294,124],{"class":112},[94,1296,116],{"class":108},[94,1298,119],{"class":112},[94,1300,127],{"class":108},[265,1302,1304],{"id":1303},"example-case-insensitive-sorting","Example: case-insensitive sorting",[85,1306,1308],{"className":87,"code":1307,"language":89,"meta":90,"style":90},"words = [\"Banana\", \"apple\", \"Cherry\"]\n\nprint(sorted(words, key=str.lower))\n\nwords.sort(key=str.lower)\nprint(words)\n",[15,1309,1310,1346,1350,1381,1385,1407],{"__ignoreMap":90},[94,1311,1312,1315,1317,1319,1321,1324,1326,1328,1330,1333,1335,1337,1339,1342,1344],{"class":96,"line":97},[94,1313,1314],{"class":100},"words ",[94,1316,105],{"class":104},[94,1318,109],{"class":108},[94,1320,332],{"class":331},[94,1322,1323],{"class":335},"Banana",[94,1325,332],{"class":331},[94,1327,116],{"class":108},[94,1329,1040],{"class":331},[94,1331,1332],{"class":335},"apple",[94,1334,332],{"class":331},[94,1336,116],{"class":108},[94,1338,1040],{"class":331},[94,1340,1341],{"class":335},"Cherry",[94,1343,332],{"class":331},[94,1345,127],{"class":108},[94,1347,1348],{"class":96,"line":130},[94,1349,134],{"emptyLinePlaceholder":133},[94,1351,1352,1354,1356,1358,1360,1363,1365,1368,1370,1373,1375,1379],{"class":96,"line":137},[94,1353,326],{"class":145},[94,1355,149],{"class":108},[94,1357,403],{"class":145},[94,1359,149],{"class":108},[94,1361,1362],{"class":152},"words",[94,1364,116],{"class":108},[94,1366,1367],{"class":1218}," key",[94,1369,105],{"class":104},[94,1371,1372],{"class":913},"str",[94,1374,168],{"class":108},[94,1376,1378],{"class":1377},"skxfh","lower",[94,1380,921],{"class":108},[94,1382,1383],{"class":96,"line":163},[94,1384,134],{"emptyLinePlaceholder":133},[94,1386,1387,1389,1391,1393,1395,1397,1399,1401,1403,1405],{"class":96,"line":323},[94,1388,1362],{"class":100},[94,1390,168],{"class":108},[94,1392,171],{"class":152},[94,1394,149],{"class":108},[94,1396,1142],{"class":1218},[94,1398,105],{"class":104},[94,1400,1372],{"class":913},[94,1402,168],{"class":108},[94,1404,1378],{"class":1377},[94,1406,316],{"class":108},[94,1408,1409,1411,1413,1415],{"class":96,"line":348},[94,1410,326],{"class":145},[94,1412,149],{"class":108},[94,1414,1362],{"class":152},[94,1416,316],{"class":108},[24,1418,1419],{},[43,1420,371],{},[85,1422,1424],{"className":87,"code":1423,"language":89,"meta":90,"style":90},"['apple', 'Banana', 'Cherry']\n['apple', 'Banana', 'Cherry']\n",[15,1425,1426,1454],{"__ignoreMap":90},[94,1427,1428,1430,1432,1434,1436,1438,1440,1442,1444,1446,1448,1450,1452],{"class":96,"line":97},[94,1429,539],{"class":108},[94,1431,968],{"class":331},[94,1433,1332],{"class":335},[94,1435,968],{"class":331},[94,1437,116],{"class":108},[94,1439,962],{"class":331},[94,1441,1323],{"class":335},[94,1443,968],{"class":331},[94,1445,116],{"class":108},[94,1447,962],{"class":331},[94,1449,1341],{"class":335},[94,1451,968],{"class":331},[94,1453,127],{"class":108},[94,1455,1456,1458,1460,1462,1464,1466,1468,1470,1472,1474,1476,1478,1480],{"class":96,"line":130},[94,1457,539],{"class":108},[94,1459,968],{"class":331},[94,1461,1332],{"class":335},[94,1463,968],{"class":331},[94,1465,116],{"class":108},[94,1467,962],{"class":331},[94,1469,1323],{"class":335},[94,1471,968],{"class":331},[94,1473,116],{"class":108},[94,1475,962],{"class":331},[94,1477,1341],{"class":335},[94,1479,968],{"class":331},[94,1481,127],{"class":108},[24,1483,1484,1485,1488],{},"Using ",[15,1486,1487],{},"key=str.lower"," tells Python to compare words in lowercase form.",[24,1490,1491,1492,168],{},"For more advanced custom sorting, especially with dictionaries, see ",[434,1493,1495],{"href":1494},"\u002Fhow-to\u002Fhow-to-sort-a-list-of-dictionaries-in-python\u002F","How to sort a list of dictionaries in Python",[80,1497,1499],{"id":1498},"beginner-mistakes-to-watch-for","Beginner mistakes to watch for",[24,1501,1502],{},"These are the most common problems beginners run into.",[265,1504,1506,1507,1509],{"id":1505},"mistake-1-assigning-listsort-to-a-variable","Mistake 1: Assigning ",[15,1508,21],{}," to a variable",[24,1511,1512],{},"This is probably the most common one.",[85,1514,1516],{"className":87,"code":1515,"language":89,"meta":90,"style":90},"numbers = [3, 1, 2]\n\nresult = numbers.sort()\n\nprint(result)\nprint(numbers)\n",[15,1517,1518,1538,1542,1556,1560,1570],{"__ignoreMap":90},[94,1519,1520,1522,1524,1526,1528,1530,1532,1534,1536],{"class":96,"line":97},[94,1521,101],{"class":100},[94,1523,105],{"class":104},[94,1525,109],{"class":108},[94,1527,113],{"class":112},[94,1529,116],{"class":108},[94,1531,119],{"class":112},[94,1533,116],{"class":108},[94,1535,124],{"class":112},[94,1537,127],{"class":108},[94,1539,1540],{"class":96,"line":130},[94,1541,134],{"emptyLinePlaceholder":133},[94,1543,1544,1546,1548,1550,1552,1554],{"class":96,"line":137},[94,1545,305],{"class":100},[94,1547,105],{"class":104},[94,1549,343],{"class":100},[94,1551,168],{"class":108},[94,1553,171],{"class":152},[94,1555,511],{"class":108},[94,1557,1558],{"class":96,"line":163},[94,1559,134],{"emptyLinePlaceholder":133},[94,1561,1562,1564,1566,1568],{"class":96,"line":323},[94,1563,326],{"class":145},[94,1565,149],{"class":108},[94,1567,902],{"class":152},[94,1569,316],{"class":108},[94,1571,1572,1574,1576,1578],{"class":96,"line":348},[94,1573,326],{"class":145},[94,1575,149],{"class":108},[94,1577,153],{"class":152},[94,1579,316],{"class":108},[24,1581,1582],{},[43,1583,371],{},[85,1585,1587],{"className":87,"code":1586,"language":89,"meta":90,"style":90},"None\n[1, 2, 3]\n",[15,1588,1589,1594],{"__ignoreMap":90},[94,1590,1591],{"class":96,"line":97},[94,1592,1593],{"class":1224},"None\n",[94,1595,1596,1598,1600,1602,1604,1606,1608],{"class":96,"line":130},[94,1597,539],{"class":108},[94,1599,412],{"class":112},[94,1601,116],{"class":108},[94,1603,124],{"class":112},[94,1605,116],{"class":108},[94,1607,421],{"class":112},[94,1609,127],{"class":108},[24,1611,1612],{},"Why this happens:",[28,1614,1615,1620,1626],{},[31,1616,1617,1619],{},[15,1618,557],{}," sorts the list in place",[31,1621,1622,1623,1625],{},"it does ",[43,1624,45],{}," return the sorted list",[31,1627,1628,1629,1631,1632],{},"so ",[15,1630,902],{}," becomes ",[15,1633,77],{},[265,1635,1637,1638,1640],{"id":1636},"mistake-2-expecting-sorted-to-change-the-original-list","Mistake 2: Expecting ",[15,1639,17],{}," to change the original list",[85,1642,1644],{"className":87,"code":1643,"language":89,"meta":90,"style":90},"numbers = [3, 1, 2]\n\nsorted(numbers)\n\nprint(numbers)\n",[15,1645,1646,1666,1670,1680,1684],{"__ignoreMap":90},[94,1647,1648,1650,1652,1654,1656,1658,1660,1662,1664],{"class":96,"line":97},[94,1649,101],{"class":100},[94,1651,105],{"class":104},[94,1653,109],{"class":108},[94,1655,113],{"class":112},[94,1657,116],{"class":108},[94,1659,119],{"class":112},[94,1661,116],{"class":108},[94,1663,124],{"class":112},[94,1665,127],{"class":108},[94,1667,1668],{"class":96,"line":130},[94,1669,134],{"emptyLinePlaceholder":133},[94,1671,1672,1674,1676,1678],{"class":96,"line":137},[94,1673,403],{"class":145},[94,1675,149],{"class":108},[94,1677,153],{"class":152},[94,1679,316],{"class":108},[94,1681,1682],{"class":96,"line":163},[94,1683,134],{"emptyLinePlaceholder":133},[94,1685,1686,1688,1690,1692],{"class":96,"line":323},[94,1687,326],{"class":145},[94,1689,149],{"class":108},[94,1691,153],{"class":152},[94,1693,316],{"class":108},[24,1695,1696],{},[43,1697,371],{},[85,1699,1701],{"className":87,"code":1700,"language":89,"meta":90,"style":90},"[3, 1, 2]\n",[15,1702,1703],{"__ignoreMap":90},[94,1704,1705,1707,1709,1711,1713,1715,1717],{"class":96,"line":97},[94,1706,539],{"class":108},[94,1708,113],{"class":112},[94,1710,116],{"class":108},[94,1712,119],{"class":112},[94,1714,116],{"class":108},[94,1716,124],{"class":112},[94,1718,127],{"class":108},[24,1720,1721,1724],{},[15,1722,1723],{},"sorted(numbers)"," creates a new sorted list, but here the result is not saved anywhere.",[24,1726,1727],{},"Correct version:",[85,1729,1731],{"className":87,"code":1730,"language":89,"meta":90,"style":90},"numbers = [3, 1, 2]\n\nnew_numbers = sorted(numbers)\n\nprint(new_numbers)\n",[15,1732,1733,1753,1757,1772,1776],{"__ignoreMap":90},[94,1734,1735,1737,1739,1741,1743,1745,1747,1749,1751],{"class":96,"line":97},[94,1736,101],{"class":100},[94,1738,105],{"class":104},[94,1740,109],{"class":108},[94,1742,113],{"class":112},[94,1744,116],{"class":108},[94,1746,119],{"class":112},[94,1748,116],{"class":108},[94,1750,124],{"class":112},[94,1752,127],{"class":108},[94,1754,1755],{"class":96,"line":130},[94,1756,134],{"emptyLinePlaceholder":133},[94,1758,1759,1762,1764,1766,1768,1770],{"class":96,"line":137},[94,1760,1761],{"class":100},"new_numbers ",[94,1763,105],{"class":104},[94,1765,146],{"class":145},[94,1767,149],{"class":108},[94,1769,153],{"class":152},[94,1771,316],{"class":108},[94,1773,1774],{"class":96,"line":163},[94,1775,134],{"emptyLinePlaceholder":133},[94,1777,1778,1780,1782,1785],{"class":96,"line":323},[94,1779,326],{"class":145},[94,1781,149],{"class":108},[94,1783,1784],{"class":152},"new_numbers",[94,1786,316],{"class":108},[265,1788,1790,1791,1793],{"id":1789},"mistake-3-trying-to-call-sort-on-a-tuple-or-string","Mistake 3: Trying to call ",[15,1792,449],{}," on a tuple or string",[85,1795,1797],{"className":87,"code":1796,"language":89,"meta":90,"style":90},"text = \"python\"\ntext.sort()\n",[15,1798,1799,1813],{"__ignoreMap":90},[94,1800,1801,1804,1806,1808,1810],{"class":96,"line":97},[94,1802,1803],{"class":100},"text ",[94,1805,105],{"class":104},[94,1807,1040],{"class":331},[94,1809,89],{"class":335},[94,1811,1812],{"class":331},"\"\n",[94,1814,1815,1818,1820,1822],{"class":96,"line":130},[94,1816,1817],{"class":100},"text",[94,1819,168],{"class":108},[94,1821,171],{"class":152},[94,1823,511],{"class":108},[24,1825,1826,1827,1829],{},"This will fail because strings do not have a ",[15,1828,449],{}," method.",[24,1831,1832,1834],{},[15,1833,449],{}," only works on lists.",[24,1836,1837,1838,1840,1841,168],{},"If you need to sort other iterable objects, use ",[15,1839,17],{},". This is easier to understand if you know what an iterable is, so you may also want to read ",[434,1842,1844],{"href":1843},"\u002Flearn\u002Fiterators-and-iterable-objects-explained\u002F","iterators and iterable objects explained",[265,1846,1848,1849,1851],{"id":1847},"mistake-4-forgetting-that-sorted-always-returns-a-list","Mistake 4: Forgetting that ",[15,1850,17],{}," always returns a list",[85,1853,1855],{"className":87,"code":1854,"language":89,"meta":90,"style":90},"text = \"cab\"\nresult = sorted(text)\n\nprint(result)\nprint(type(result))\n",[15,1856,1857,1870,1884,1888,1898],{"__ignoreMap":90},[94,1858,1859,1861,1863,1865,1868],{"class":96,"line":97},[94,1860,1803],{"class":100},[94,1862,105],{"class":104},[94,1864,1040],{"class":331},[94,1866,1867],{"class":335},"cab",[94,1869,1812],{"class":331},[94,1871,1872,1874,1876,1878,1880,1882],{"class":96,"line":130},[94,1873,305],{"class":100},[94,1875,105],{"class":104},[94,1877,146],{"class":145},[94,1879,149],{"class":108},[94,1881,1817],{"class":152},[94,1883,316],{"class":108},[94,1885,1886],{"class":96,"line":137},[94,1887,134],{"emptyLinePlaceholder":133},[94,1889,1890,1892,1894,1896],{"class":96,"line":163},[94,1891,326],{"class":145},[94,1893,149],{"class":108},[94,1895,902],{"class":152},[94,1897,316],{"class":108},[94,1899,1900,1902,1904,1906,1908,1910],{"class":96,"line":323},[94,1901,326],{"class":145},[94,1903,149],{"class":108},[94,1905,914],{"class":913},[94,1907,149],{"class":108},[94,1909,902],{"class":152},[94,1911,921],{"class":108},[24,1913,1914],{},[43,1915,371],{},[85,1917,1919],{"className":87,"code":1918,"language":89,"meta":90,"style":90},"['a', 'b', 'c']\n\u003Cclass 'list'>\n",[15,1920,1921,1951],{"__ignoreMap":90},[94,1922,1923,1925,1927,1929,1931,1933,1935,1938,1940,1942,1944,1947,1949],{"class":96,"line":97},[94,1924,539],{"class":108},[94,1926,968],{"class":331},[94,1928,434],{"class":335},[94,1930,968],{"class":331},[94,1932,116],{"class":108},[94,1934,962],{"class":331},[94,1936,1937],{"class":335},"b",[94,1939,968],{"class":331},[94,1941,116],{"class":108},[94,1943,962],{"class":331},[94,1945,1946],{"class":335},"c",[94,1948,968],{"class":331},[94,1950,127],{"class":108},[94,1952,1953,1955,1957,1959,1961,1963],{"class":96,"line":130},[94,1954,955],{"class":104},[94,1956,959],{"class":958},[94,1958,962],{"class":331},[94,1960,965],{"class":335},[94,1962,968],{"class":331},[94,1964,971],{"class":104},[24,1966,1967],{},"Even though the original value was a string, the result is still a list.",[80,1969,1971],{"id":1970},"common-causes-of-confusion","Common causes of confusion",[24,1973,1974],{},"Beginners usually mix these up for one of these reasons:",[28,1976,1977,1980,1986,1991,1997],{},[31,1978,1979],{},"confusing a function with a method",[31,1981,1982,1983,1985],{},"not knowing that ",[15,1984,449],{}," changes the list in place",[31,1987,1988,1989,1509],{},"assigning the result of ",[15,1990,21],{},[31,1992,1993,1994,1996],{},"trying to use ",[15,1995,449],{}," on non-list data types",[31,1998,1999,2000,2002],{},"expecting ",[15,2001,17],{}," to preserve the original data type, such as tuple or string",[24,2004,2005],{},"If something is not behaving the way you expect, these quick checks can help:",[85,2007,2009],{"className":87,"code":2008,"language":89,"meta":90,"style":90},"print(my_list)\nprint(sorted(my_list))\nresult = my_list.sort(); print(result)\nprint(type(my_data))\nhelp(sorted)\nhelp(list.sort)\n",[15,2010,2011,2021,2035,2061,2076,2087],{"__ignoreMap":90},[94,2012,2013,2015,2017,2019],{"class":96,"line":97},[94,2014,326],{"class":145},[94,2016,149],{"class":108},[94,2018,594],{"class":152},[94,2020,316],{"class":108},[94,2022,2023,2025,2027,2029,2031,2033],{"class":96,"line":130},[94,2024,326],{"class":145},[94,2026,149],{"class":108},[94,2028,403],{"class":145},[94,2030,149],{"class":108},[94,2032,594],{"class":152},[94,2034,921],{"class":108},[94,2036,2037,2039,2041,2044,2046,2048,2050,2053,2055,2057,2059],{"class":96,"line":137},[94,2038,305],{"class":100},[94,2040,105],{"class":104},[94,2042,2043],{"class":100}," my_list",[94,2045,168],{"class":108},[94,2047,171],{"class":152},[94,2049,174],{"class":108},[94,2051,2052],{"class":100},"; ",[94,2054,326],{"class":145},[94,2056,149],{"class":108},[94,2058,902],{"class":152},[94,2060,316],{"class":108},[94,2062,2063,2065,2067,2069,2071,2074],{"class":96,"line":163},[94,2064,326],{"class":145},[94,2066,149],{"class":108},[94,2068,914],{"class":913},[94,2070,149],{"class":108},[94,2072,2073],{"class":152},"my_data",[94,2075,921],{"class":108},[94,2077,2078,2081,2083,2085],{"class":96,"line":323},[94,2079,2080],{"class":145},"help",[94,2082,149],{"class":108},[94,2084,403],{"class":145},[94,2086,316],{"class":108},[94,2088,2089,2091,2093,2095,2097,2099],{"class":96,"line":348},[94,2090,2080],{"class":145},[94,2092,149],{"class":108},[94,2094,965],{"class":913},[94,2096,168],{"class":108},[94,2098,171],{"class":1377},[94,2100,316],{"class":108},[24,2102,2103],{},"These are useful for checking:",[28,2105,2106,2109,2115,2123],{},[31,2107,2108],{},"whether your original list changed",[31,2110,2111,2112,2114],{},"what ",[15,2113,17],{}," returns",[31,2116,2117,2118,2120,2121],{},"whether ",[15,2119,449],{}," returned ",[15,2122,77],{},[31,2124,2125],{},"what type of object you are working with",[80,2127,2129],{"id":2128},"simple-rule-to-remember","Simple rule to remember",[24,2131,2132],{},"Use this rule:",[28,2134,2135,2143],{},[31,2136,2137,2138,2140,2141],{},"Need a ",[43,2139,60],{},": use ",[15,2142,17],{},[31,2144,2145,2146,2140,2149],{},"Need to ",[43,2147,2148],{},"change one list directly",[15,2150,21],{},[24,2152,2153,2154,2156],{},"For many beginners, ",[15,2155,17],{}," feels easier because it is more explicit and does not silently change the original list.",[80,2158,2160],{"id":2159},"faq","FAQ",[265,2162,2164,2165,2167,2168,2170],{"id":2163},"which-is-better-for-beginners-sorted-or-listsort","Which is better for beginners, ",[15,2166,17],{}," or ",[15,2169,21],{},"?",[24,2172,2173,2175],{},[15,2174,17],{}," is often easier to understand because it returns a new result and does not change the original data.",[265,2177,2179,2180,2182,2183,2170],{"id":2178},"why-does-listsort-return-none","Why does ",[15,2181,21],{}," return ",[15,2184,77],{},[24,2186,2187],{},"Because it changes the list directly instead of creating and returning a new sorted list.",[265,2189,2191,2192,2194],{"id":2190},"can-sorted-sort-a-tuple","Can ",[15,2193,17],{}," sort a tuple?",[24,2196,2197],{},"Yes. It can sort any iterable, but it returns a list.",[265,2199,2201,2202,2204],{"id":2200},"can-i-use-sort-on-a-string","Can I use ",[15,2203,449],{}," on a string?",[24,2206,2207,2208,2210,2211,2214],{},"No. ",[15,2209,449],{}," is a list method. Use ",[15,2212,2213],{},"sorted(string)"," if needed.",[265,2216,2218,2219,1139,2221,2223],{"id":2217},"do-sorted-and-sort-both-support-descending-order","Do ",[15,2220,17],{},[15,2222,449],{}," both support descending order?",[24,2225,2226,2227,168],{},"Yes. Both support ",[15,2228,1158],{},[80,2230,2232],{"id":2231},"see-also","See also",[28,2234,2235,2241,2247,2251,2255],{},[31,2236,2237],{},[434,2238,13,2239,439],{"href":436},[15,2240,17],{},[31,2242,2243],{},[434,2244,13,2245,567],{"href":564},[15,2246,21],{},[31,2248,2249],{},[434,2250,1132],{"href":1131},[31,2252,2253],{},[434,2254,1495],{"href":1494},[31,2256,2257],{},[434,2258,986],{"href":985},[2260,2261,2262],"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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .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}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":90,"searchDepth":130,"depth":130,"links":2264},[2265,2266,2267,2272,2277,2280,2284,2288,2293,2303,2304,2305,2317],{"id":82,"depth":130,"text":83},{"id":188,"depth":130,"text":189},{"id":214,"depth":130,"text":2268,"children":2269},"What sorted() does",[2270],{"id":267,"depth":137,"text":2271},"Example: sorted() keeps the original list unchanged",{"id":442,"depth":130,"text":2273,"children":2274},"What list.sort() does",[2275],{"id":468,"depth":137,"text":2276},"Example: list.sort() changes the original list",{"id":570,"depth":130,"text":571,"children":2278},[2279],{"id":598,"depth":137,"text":599},{"id":814,"depth":130,"text":2281,"children":2282},"When to use sorted()",[2283],{"id":839,"depth":137,"text":840},{"id":990,"depth":130,"text":2285,"children":2286},"When to use list.sort()",[2287],{"id":1013,"depth":137,"text":1014},{"id":1135,"depth":130,"text":2289,"children":2290},"reverse and key work with both",[2291,2292],{"id":1171,"depth":137,"text":1172},{"id":1303,"depth":137,"text":1304},{"id":1498,"depth":130,"text":1499,"children":2294},[2295,2297,2299,2301],{"id":1505,"depth":137,"text":2296},"Mistake 1: Assigning list.sort() to a variable",{"id":1636,"depth":137,"text":2298},"Mistake 2: Expecting sorted() to change the original list",{"id":1789,"depth":137,"text":2300},"Mistake 3: Trying to call sort() on a tuple or string",{"id":1847,"depth":137,"text":2302},"Mistake 4: Forgetting that sorted() always returns a list",{"id":1970,"depth":130,"text":1971},{"id":2128,"depth":130,"text":2129},{"id":2159,"depth":130,"text":2160,"children":2306},[2307,2309,2311,2313,2315],{"id":2163,"depth":137,"text":2308},"Which is better for beginners, sorted() or list.sort()?",{"id":2178,"depth":137,"text":2310},"Why does list.sort() return None?",{"id":2190,"depth":137,"text":2312},"Can sorted() sort a tuple?",{"id":2200,"depth":137,"text":2314},"Can I use sort() on a string?",{"id":2217,"depth":137,"text":2316},"Do sorted() and sort() both support descending order?",{"id":2231,"depth":130,"text":2232},"Master python sorted vs list.sort explained in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-sorted-vs-list.sort-explained",{"title":5,"description":2318},"reference\u002Fpython-sorted-vs-list.sort-explained","X_9GbXWbpLil07A8gxuHLi2EoRTNXkAwL_w70GVpp14",1777585491726]