[{"data":1,"prerenderedAt":847},["ShallowReactive",2],{"blog-\u002Fblog\u002Fpython-lists-slicing-explained":3},{"id":4,"title":5,"body":6,"description":833,"difficulty":834,"extension":835,"framework":836,"frameworkSlug":59,"meta":837,"navigation":429,"order":67,"path":838,"qaPath":839,"seo":840,"stem":841,"subtopic":842,"topic":843,"topicSlug":844,"updated":845,"__hash__":846},"blog\u002Fblog\u002Fpython-lists-slicing-explained.md","Python Lists and Slicing Explained — append vs extend, sort vs sorted, and Negative Steps",{"type":7,"value":8,"toc":823},"minimark",[9,14,38,42,54,108,115,119,134,267,278,282,290,372,376,386,502,515,519,541,625,639,712,716,727,770,777,781,819],[10,11,13],"h2",{"id":12},"python-lists-explained","Python lists, explained",[15,16,17,18,22,23,22,26,29,30,33,34,37],"p",{},"The list is Python's workhorse sequence, and interviewers use it to check whether you\nunderstand slicing, in-place vs returning operations, and the cost of the operations you\nreach for. This guide covers slicing mechanics, the ",[19,20,21],"code",{},"append","\u002F",[19,24,25],{},"extend",[19,27,28],{},"insert"," distinction,\nand ",[19,31,32],{},"sort"," vs ",[19,35,36],{},"sorted",".",[10,39,41],{"id":40},"lists-vs-arrays","Lists vs arrays",[15,43,44,45,48,49,53],{},"A Python ",[19,46,47],{},"list"," is a ",[50,51,52],"strong",{},"dynamic, heterogeneous"," sequence of object references — it can hold\nmixed types and resize automatically:",[55,56,61],"pre",{"className":57,"code":58,"language":59,"meta":60,"style":60},"language-python shiki shiki-themes github-light github-dark","mixed = [1, \"two\", 3.0, [4]]   # perfectly legal\n","python","",[19,62,63],{"__ignoreMap":60},[64,65,68,72,76,79,83,86,90,92,95,98,101,104],"span",{"class":66,"line":67},"line",1,[64,69,71],{"class":70},"sVt8B","mixed ",[64,73,75],{"class":74},"szBVR","=",[64,77,78],{"class":70}," [",[64,80,82],{"class":81},"sj4cs","1",[64,84,85],{"class":70},", ",[64,87,89],{"class":88},"sZZnC","\"two\"",[64,91,85],{"class":70},[64,93,94],{"class":81},"3.0",[64,96,97],{"class":70},", [",[64,99,100],{"class":81},"4",[64,102,103],{"class":70},"]]   ",[64,105,107],{"class":106},"sJ8bj","# perfectly legal\n",[15,109,110,111,114],{},"It is not a contiguous block of numbers like a C array. For large numeric data, use\n",[19,112,113],{},"array.array"," (typed) or, in practice, NumPy arrays, which store raw values compactly and\nrun far faster for math.",[10,116,118],{"id":117},"slicing-including-negative-steps","Slicing, including negative steps",[15,120,121,122,125,126,129,130,133],{},"Slicing uses ",[19,123,124],{},"[start:stop:step]",", where ",[19,127,128],{},"stop"," is ",[50,131,132],{},"exclusive"," and any part can be\nomitted:",[55,135,137],{"className":57,"code":136,"language":59,"meta":60,"style":60},"nums = [0, 1, 2, 3, 4, 5]\nnums[1:4]      # [1, 2, 3]\nnums[:3]       # [0, 1, 2]\nnums[2:]       # [2, 3, 4, 5]\nnums[::2]      # [0, 2, 4]      — every other element\nnums[-2:]      # [4, 5]         — last two\nnums[::-1]     # [5, 4, 3, 2, 1, 0]  — reversed\n",[19,138,139,177,196,210,223,236,252],{"__ignoreMap":60},[64,140,141,144,146,148,151,153,155,157,160,162,165,167,169,171,174],{"class":66,"line":67},[64,142,143],{"class":70},"nums ",[64,145,75],{"class":74},[64,147,78],{"class":70},[64,149,150],{"class":81},"0",[64,152,85],{"class":70},[64,154,82],{"class":81},[64,156,85],{"class":70},[64,158,159],{"class":81},"2",[64,161,85],{"class":70},[64,163,164],{"class":81},"3",[64,166,85],{"class":70},[64,168,100],{"class":81},[64,170,85],{"class":70},[64,172,173],{"class":81},"5",[64,175,176],{"class":70},"]\n",[64,178,180,183,185,188,190,193],{"class":66,"line":179},2,[64,181,182],{"class":70},"nums[",[64,184,82],{"class":81},[64,186,187],{"class":70},":",[64,189,100],{"class":81},[64,191,192],{"class":70},"]      ",[64,194,195],{"class":106},"# [1, 2, 3]\n",[64,197,199,202,204,207],{"class":66,"line":198},3,[64,200,201],{"class":70},"nums[:",[64,203,164],{"class":81},[64,205,206],{"class":70},"]       ",[64,208,209],{"class":106},"# [0, 1, 2]\n",[64,211,213,215,217,220],{"class":66,"line":212},4,[64,214,182],{"class":70},[64,216,159],{"class":81},[64,218,219],{"class":70},":]       ",[64,221,222],{"class":106},"# [2, 3, 4, 5]\n",[64,224,226,229,231,233],{"class":66,"line":225},5,[64,227,228],{"class":70},"nums[::",[64,230,159],{"class":81},[64,232,192],{"class":70},[64,234,235],{"class":106},"# [0, 2, 4]      — every other element\n",[64,237,239,241,244,246,249],{"class":66,"line":238},6,[64,240,182],{"class":70},[64,242,243],{"class":74},"-",[64,245,159],{"class":81},[64,247,248],{"class":70},":]      ",[64,250,251],{"class":106},"# [4, 5]         — last two\n",[64,253,255,257,259,261,264],{"class":66,"line":254},7,[64,256,228],{"class":70},[64,258,243],{"class":74},[64,260,82],{"class":81},[64,262,263],{"class":70},"]     ",[64,265,266],{"class":106},"# [5, 4, 3, 2, 1, 0]  — reversed\n",[15,268,269,270,273,274,277],{},"A ",[50,271,272],{},"slice creates a shallow copy"," — ",[19,275,276],{},"nums[:]"," is a common way to copy a list. The copy is\nshallow, so nested objects are shared, not duplicated.",[10,279,281],{"id":280},"slice-assignment","Slice assignment",[15,283,284,285,289],{},"You can assign ",[286,287,288],"em",{},"to"," a slice to replace, insert, or delete a whole region in place:",[55,291,293],{"className":57,"code":292,"language":59,"meta":60,"style":60},"nums = [0, 1, 2, 3, 4]\nnums[1:3] = [\"a\", \"b\", \"c\"]    # [0, 'a', 'b', 'c', 3, 4] — replace with different length\nnums[:] = []                   # clears the list in place\n",[19,294,295,323,359],{"__ignoreMap":60},[64,296,297,299,301,303,305,307,309,311,313,315,317,319,321],{"class":66,"line":67},[64,298,143],{"class":70},[64,300,75],{"class":74},[64,302,78],{"class":70},[64,304,150],{"class":81},[64,306,85],{"class":70},[64,308,82],{"class":81},[64,310,85],{"class":70},[64,312,159],{"class":81},[64,314,85],{"class":70},[64,316,164],{"class":81},[64,318,85],{"class":70},[64,320,100],{"class":81},[64,322,176],{"class":70},[64,324,325,327,329,331,333,336,338,340,343,345,348,350,353,356],{"class":66,"line":179},[64,326,182],{"class":70},[64,328,82],{"class":81},[64,330,187],{"class":70},[64,332,164],{"class":81},[64,334,335],{"class":70},"] ",[64,337,75],{"class":74},[64,339,78],{"class":70},[64,341,342],{"class":88},"\"a\"",[64,344,85],{"class":70},[64,346,347],{"class":88},"\"b\"",[64,349,85],{"class":70},[64,351,352],{"class":88},"\"c\"",[64,354,355],{"class":70},"]    ",[64,357,358],{"class":106},"# [0, 'a', 'b', 'c', 3, 4] — replace with different length\n",[64,360,361,364,366,369],{"class":66,"line":198},[64,362,363],{"class":70},"nums[:] ",[64,365,75],{"class":74},[64,367,368],{"class":70}," []                   ",[64,370,371],{"class":106},"# clears the list in place\n",[10,373,375],{"id":374},"append-vs-extend-vs-insert","append vs extend vs insert",[15,377,378,379,382,383,187],{},"These three add elements but differ in ",[286,380,381],{},"what"," and ",[286,384,385],{},"where",[55,387,389],{"className":57,"code":388,"language":59,"meta":60,"style":60},"lst = [1, 2]\nlst.append([3, 4])   # [1, 2, [3, 4]]  — adds ONE element (the list itself)\n\nlst = [1, 2]\nlst.extend([3, 4])   # [1, 2, 3, 4]    — adds each element of the iterable\n\nlst = [1, 2]\nlst.insert(0, 99)    # [99, 1, 2]      — insert at an index (O(n): shifts elements)\n",[19,390,391,408,425,431,447,463,467,483],{"__ignoreMap":60},[64,392,393,396,398,400,402,404,406],{"class":66,"line":67},[64,394,395],{"class":70},"lst ",[64,397,75],{"class":74},[64,399,78],{"class":70},[64,401,82],{"class":81},[64,403,85],{"class":70},[64,405,159],{"class":81},[64,407,176],{"class":70},[64,409,410,413,415,417,419,422],{"class":66,"line":179},[64,411,412],{"class":70},"lst.append([",[64,414,164],{"class":81},[64,416,85],{"class":70},[64,418,100],{"class":81},[64,420,421],{"class":70},"])   ",[64,423,424],{"class":106},"# [1, 2, [3, 4]]  — adds ONE element (the list itself)\n",[64,426,427],{"class":66,"line":198},[64,428,430],{"emptyLinePlaceholder":429},true,"\n",[64,432,433,435,437,439,441,443,445],{"class":66,"line":212},[64,434,395],{"class":70},[64,436,75],{"class":74},[64,438,78],{"class":70},[64,440,82],{"class":81},[64,442,85],{"class":70},[64,444,159],{"class":81},[64,446,176],{"class":70},[64,448,449,452,454,456,458,460],{"class":66,"line":225},[64,450,451],{"class":70},"lst.extend([",[64,453,164],{"class":81},[64,455,85],{"class":70},[64,457,100],{"class":81},[64,459,421],{"class":70},[64,461,462],{"class":106},"# [1, 2, 3, 4]    — adds each element of the iterable\n",[64,464,465],{"class":66,"line":238},[64,466,430],{"emptyLinePlaceholder":429},[64,468,469,471,473,475,477,479,481],{"class":66,"line":254},[64,470,395],{"class":70},[64,472,75],{"class":74},[64,474,78],{"class":70},[64,476,82],{"class":81},[64,478,85],{"class":70},[64,480,159],{"class":81},[64,482,176],{"class":70},[64,484,486,489,491,493,496,499],{"class":66,"line":485},8,[64,487,488],{"class":70},"lst.insert(",[64,490,150],{"class":81},[64,492,85],{"class":70},[64,494,495],{"class":81},"99",[64,497,498],{"class":70},")    ",[64,500,501],{"class":106},"# [99, 1, 2]      — insert at an index (O(n): shifts elements)\n",[15,503,504,506,507,510,511,514],{},[19,505,21],{}," is O(1) amortised; ",[19,508,509],{},"insert(0, x)"," is O(n) because everything shifts. If you\nfrequently add to the front, use ",[19,512,513],{},"collections.deque"," instead.",[10,516,518],{"id":517},"sort-vs-sorted","sort vs sorted",[15,520,521,524,525,528,529,532,533,536,537,540],{},[19,522,523],{},"list.sort()"," sorts ",[50,526,527],{},"in place"," and returns ",[19,530,531],{},"None","; ",[19,534,535],{},"sorted()"," returns a ",[50,538,539],{},"new"," sorted\nlist and works on any iterable:",[55,542,544],{"className":57,"code":543,"language":59,"meta":60,"style":60},"nums = [3, 1, 2]\nnums.sort()              # nums is now [1, 2, 3]; returns None\nresult = nums.sort()     # BUG: result is None\n\nsquared = sorted([3, 1, 2])   # [1, 2, 3], original untouched\n",[19,545,546,566,574,593,597],{"__ignoreMap":60},[64,547,548,550,552,554,556,558,560,562,564],{"class":66,"line":67},[64,549,143],{"class":70},[64,551,75],{"class":74},[64,553,78],{"class":70},[64,555,164],{"class":81},[64,557,85],{"class":70},[64,559,82],{"class":81},[64,561,85],{"class":70},[64,563,159],{"class":81},[64,565,176],{"class":70},[64,567,568,571],{"class":66,"line":179},[64,569,570],{"class":70},"nums.sort()              ",[64,572,573],{"class":106},"# nums is now [1, 2, 3]; returns None\n",[64,575,576,579,581,584,587,590],{"class":66,"line":198},[64,577,578],{"class":70},"result ",[64,580,75],{"class":74},[64,582,583],{"class":70}," nums.sort()     ",[64,585,586],{"class":106},"# ",[64,588,589],{"class":74},"BUG",[64,591,592],{"class":106},": result is None\n",[64,594,595],{"class":66,"line":212},[64,596,430],{"emptyLinePlaceholder":429},[64,598,599,602,604,607,610,612,614,616,618,620,622],{"class":66,"line":225},[64,600,601],{"class":70},"squared ",[64,603,75],{"class":74},[64,605,606],{"class":81}," sorted",[64,608,609],{"class":70},"([",[64,611,164],{"class":81},[64,613,85],{"class":70},[64,615,82],{"class":81},[64,617,85],{"class":70},[64,619,159],{"class":81},[64,621,421],{"class":70},[64,623,624],{"class":106},"# [1, 2, 3], original untouched\n",[15,626,627,628,382,631,634,635,638],{},"Both accept ",[19,629,630],{},"key",[19,632,633],{},"reverse",", and both are ",[50,636,637],{},"stable"," (equal elements keep their order):",[55,640,642],{"className":57,"code":641,"language":59,"meta":60,"style":60},"words = [\"bb\", \"a\", \"ccc\"]\nsorted(words, key=len)            # ['a', 'bb', 'ccc']\nsorted(words, key=len, reverse=True)\n",[19,643,644,667,688],{"__ignoreMap":60},[64,645,646,649,651,653,656,658,660,662,665],{"class":66,"line":67},[64,647,648],{"class":70},"words ",[64,650,75],{"class":74},[64,652,78],{"class":70},[64,654,655],{"class":88},"\"bb\"",[64,657,85],{"class":70},[64,659,342],{"class":88},[64,661,85],{"class":70},[64,663,664],{"class":88},"\"ccc\"",[64,666,176],{"class":70},[64,668,669,671,674,677,679,682,685],{"class":66,"line":179},[64,670,36],{"class":81},[64,672,673],{"class":70},"(words, ",[64,675,630],{"class":676},"s4XuR",[64,678,75],{"class":74},[64,680,681],{"class":81},"len",[64,683,684],{"class":70},")            ",[64,686,687],{"class":106},"# ['a', 'bb', 'ccc']\n",[64,689,690,692,694,696,698,700,702,704,706,709],{"class":66,"line":198},[64,691,36],{"class":81},[64,693,673],{"class":70},[64,695,630],{"class":676},[64,697,75],{"class":74},[64,699,681],{"class":81},[64,701,85],{"class":70},[64,703,633],{"class":676},[64,705,75],{"class":74},[64,707,708],{"class":81},"True",[64,710,711],{"class":70},")\n",[10,713,715],{"id":714},"are-comprehensions-faster-than-loops","Are comprehensions faster than loops?",[15,717,718,719,722,723,726],{},"Yes, modestly — a list comprehension is usually faster than the equivalent ",[19,720,721],{},"for"," loop with\n",[19,724,725],{},".append()",", because the iteration and appending happen in C rather than via repeated\nattribute lookups and method calls:",[55,728,730],{"className":57,"code":729,"language":59,"meta":60,"style":60},"squares = [x * x for x in range(1000)]   # faster + clearer than a loop\n",[19,731,732],{"__ignoreMap":60},[64,733,734,737,739,742,745,748,750,752,755,758,761,764,767],{"class":66,"line":67},[64,735,736],{"class":70},"squares ",[64,738,75],{"class":74},[64,740,741],{"class":70}," [x ",[64,743,744],{"class":74},"*",[64,746,747],{"class":70}," x ",[64,749,721],{"class":74},[64,751,747],{"class":70},[64,753,754],{"class":74},"in",[64,756,757],{"class":81}," range",[64,759,760],{"class":70},"(",[64,762,763],{"class":81},"1000",[64,765,766],{"class":70},")]   ",[64,768,769],{"class":106},"# faster + clearer than a loop\n",[15,771,772,773,776],{},"Use a comprehension when you're ",[286,774,775],{},"building a list","; use a plain loop when you're doing it for\nside effects.",[10,778,780],{"id":779},"recap","Recap",[15,782,783,784,786,787,789,790,793,794,797,798,800,801,803,804,806,807,809,810,812,813,815,816,818],{},"A list is a resizable, heterogeneous sequence of references — reach for NumPy when you need\nfast numeric arrays. Slicing ",[19,785,124],{}," is exclusive of ",[19,788,128],{},", supports negative\nsteps (",[19,791,792],{},"[::-1]"," reverses), and returns a ",[50,795,796],{},"shallow copy",". ",[19,799,21],{}," adds a single item,\n",[19,802,25],{}," adds each item of an iterable, and ",[19,805,28],{}," is O(n). Use ",[19,808,523],{}," for an\nin-place sort (returns ",[19,811,531],{},") and ",[19,814,535],{}," to get a new list; both are stable and take a\n",[19,817,630],{},". Prefer comprehensions when building lists.",[820,821,822],"style",{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}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 .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":60,"searchDepth":179,"depth":179,"links":824},[825,826,827,828,829,830,831,832],{"id":12,"depth":179,"text":13},{"id":40,"depth":179,"text":41},{"id":117,"depth":179,"text":118},{"id":280,"depth":179,"text":281},{"id":374,"depth":179,"text":375},{"id":517,"depth":179,"text":518},{"id":714,"depth":179,"text":715},{"id":779,"depth":179,"text":780},"How Python lists work — slicing with negative steps, the difference between append, extend, and insert, list.sort vs sorted, and when a list is the wrong tool.","medium","md","Python",{},"\u002Fblog\u002Fpython-lists-slicing-explained","\u002Fpython\u002Fdata-structures\u002Flists",{"title":5,"description":833},"blog\u002Fpython-lists-slicing-explained","Lists & Slicing","Data Structures","data-structures","2026-06-19","Bs2ZA6INESUCtkGaDVyoMLHrbQDErQEv0s_KNd9CSqY",1782244092256]