[{"data":1,"prerenderedAt":3264},["ShallowReactive",2],{"page-\u002Fpython\u002F07-lists-and-tuples":3},{"id":4,"title":5,"body":6,"description":27,"extension":3258,"meta":3259,"navigation":49,"path":3260,"seo":3261,"stem":3262,"__hash__":3263},"content\u002Fpython\u002F07-lists-and-tuples.md","07 — Lists & Tuples",{"type":7,"value":8,"toc":3240},"minimark",[9,13,18,478,887,891,1111,1116,1301,1305,1565,1569,1573,1754,1758,1987,1991,2061,2065,2088,2183,2187,2402,2425,2561,2565,2686,2708,2784,2788,2871,2875,2990,2994,2997,3051,3157,3161,3236],[10,11,5],"h1",{"id":12},"_07-lists-tuples",[14,15,17],"h2",{"id":16},"memory-layout-and-amortized-append-why-lists-are-fast","Memory Layout and Amortized Append — Why Lists Are Fast",[19,20,22],"code-wrapper",{"language":21},"python",[23,24,28],"pre",{"className":25,"code":26,"language":21,"meta":27,"style":27},"language-python shiki shiki-themes github-light github-dark","import sys\n\n# ── List over-allocation: lists pre-grow to amortize append to O(1) ──\n# A list of N elements has capacity > N — the underlying PyObject*[] array\n# is over-allocated by ~(N\u002F8 + 3) slots, so most appends don't trigger a realloc.\n\ndef show_capacity(n: int):\n    \"\"\"Build a list of n elements, show its actual byte size and over-allocation.\"\"\"\n    lst = []\n    for i in range(n):\n        lst.append(i)\n    # sys.getsizeof reports the list object size, NOT the pointed-to objects\n    # Each slot is 8 bytes (a pointer) on 64-bit CPython\n    size = sys.getsizeof(lst)\n    slots = (size - sys.getsizeof([])) \u002F\u002F 8   # subtract list header, divide by pointer size\n    print(f\"len={len(lst):>6}  sizeof={size:>6}  capacity≈{slots:>6}  overalloc={slots - len(lst):>4}\")\n\nshow_capacity(0)      # len=     0  capacity≈     0  — empty list, no pointer array\nshow_capacity(10)     # len=    10  capacity≈    16  — 6 spare slots\nshow_capacity(100)    # len=   100  capacity≈   116  — ~16% over-allocated\nshow_capacity(1000)   # len=  1000  capacity≈  1030  — ~3% over-allocated (ratio shrinks)\n\n# ── Tuple: no over-allocation (immutable, no growth needed) → smaller footprint ──\nlst = [1, 2, 3, 4, 5]\ntup = (1, 2, 3, 4, 5)\nprint(f\"list sizeof: {sys.getsizeof(lst)}\")   # ~120 bytes — header + 8 spare pointer slots\nprint(f\"tuple sizeof: {sys.getsizeof(tup)}\")  # ~64 bytes — header + exactly 5 slots, no spare\n\n# Tuples of constants can be folded into the code object's co_consts at compile time\n# (a \"constant tuple\" optimization) — the tuple is created once, reused on each access\n","",[29,30,31,44,51,58,64,70,75,95,102,114,132,138,144,150,161,187,265,270,285,299,313,327,332,338,376,407,434,461,466,472],"code",{"__ignoreMap":27},[32,33,36,40],"span",{"class":34,"line":35},"line",1,[32,37,39],{"class":38},"svdQ7","import",[32,41,43],{"class":42},"ssxIu"," sys\n",[32,45,47],{"class":34,"line":46},2,[32,48,50],{"emptyLinePlaceholder":49},true,"\n",[32,52,54],{"class":34,"line":53},3,[32,55,57],{"class":56},"sdCPZ","# ── List over-allocation: lists pre-grow to amortize append to O(1) ──\n",[32,59,61],{"class":34,"line":60},4,[32,62,63],{"class":56},"# A list of N elements has capacity > N — the underlying PyObject*[] array\n",[32,65,67],{"class":34,"line":66},5,[32,68,69],{"class":56},"# is over-allocated by ~(N\u002F8 + 3) slots, so most appends don't trigger a realloc.\n",[32,71,73],{"class":34,"line":72},6,[32,74,50],{"emptyLinePlaceholder":49},[32,76,78,81,85,88,92],{"class":34,"line":77},7,[32,79,80],{"class":38},"def",[32,82,84],{"class":83},"sIsaT"," show_capacity",[32,86,87],{"class":42},"(n: ",[32,89,91],{"class":90},"snvgF","int",[32,93,94],{"class":42},"):\n",[32,96,98],{"class":34,"line":97},8,[32,99,101],{"class":100},"sJ6F3","    \"\"\"Build a list of n elements, show its actual byte size and over-allocation.\"\"\"\n",[32,103,105,108,111],{"class":34,"line":104},9,[32,106,107],{"class":42},"    lst ",[32,109,110],{"class":38},"=",[32,112,113],{"class":42}," []\n",[32,115,117,120,123,126,129],{"class":34,"line":116},10,[32,118,119],{"class":38},"    for",[32,121,122],{"class":42}," i ",[32,124,125],{"class":38},"in",[32,127,128],{"class":90}," range",[32,130,131],{"class":42},"(n):\n",[32,133,135],{"class":34,"line":134},11,[32,136,137],{"class":42},"        lst.append(i)\n",[32,139,141],{"class":34,"line":140},12,[32,142,143],{"class":56},"    # sys.getsizeof reports the list object size, NOT the pointed-to objects\n",[32,145,147],{"class":34,"line":146},13,[32,148,149],{"class":56},"    # Each slot is 8 bytes (a pointer) on 64-bit CPython\n",[32,151,153,156,158],{"class":34,"line":152},14,[32,154,155],{"class":42},"    size ",[32,157,110],{"class":38},[32,159,160],{"class":42}," sys.getsizeof(lst)\n",[32,162,164,167,169,172,175,178,181,184],{"class":34,"line":163},15,[32,165,166],{"class":42},"    slots ",[32,168,110],{"class":38},[32,170,171],{"class":42}," (size ",[32,173,174],{"class":38},"-",[32,176,177],{"class":42}," sys.getsizeof([])) ",[32,179,180],{"class":38},"\u002F\u002F",[32,182,183],{"class":90}," 8",[32,185,186],{"class":56},"   # subtract list header, divide by pointer size\n",[32,188,190,193,196,199,202,205,208,211,214,217,220,223,225,227,230,232,235,237,239,242,244,247,249,252,254,257,259,262],{"class":34,"line":189},16,[32,191,192],{"class":90},"    print",[32,194,195],{"class":42},"(",[32,197,198],{"class":38},"f",[32,200,201],{"class":100},"\"len=",[32,203,204],{"class":90},"{len",[32,206,207],{"class":42},"(lst)",[32,209,210],{"class":38},":>6",[32,212,213],{"class":90},"}",[32,215,216],{"class":100},"  sizeof=",[32,218,219],{"class":90},"{",[32,221,222],{"class":42},"size",[32,224,210],{"class":38},[32,226,213],{"class":90},[32,228,229],{"class":100},"  capacity≈",[32,231,219],{"class":90},[32,233,234],{"class":42},"slots",[32,236,210],{"class":38},[32,238,213],{"class":90},[32,240,241],{"class":100},"  overalloc=",[32,243,219],{"class":90},[32,245,246],{"class":42},"slots ",[32,248,174],{"class":38},[32,250,251],{"class":90}," len",[32,253,207],{"class":42},[32,255,256],{"class":38},":>4",[32,258,213],{"class":90},[32,260,261],{"class":100},"\"",[32,263,264],{"class":42},")\n",[32,266,268],{"class":34,"line":267},17,[32,269,50],{"emptyLinePlaceholder":49},[32,271,273,276,279,282],{"class":34,"line":272},18,[32,274,275],{"class":42},"show_capacity(",[32,277,278],{"class":90},"0",[32,280,281],{"class":42},")      ",[32,283,284],{"class":56},"# len=     0  capacity≈     0  — empty list, no pointer array\n",[32,286,288,290,293,296],{"class":34,"line":287},19,[32,289,275],{"class":42},[32,291,292],{"class":90},"10",[32,294,295],{"class":42},")     ",[32,297,298],{"class":56},"# len=    10  capacity≈    16  — 6 spare slots\n",[32,300,302,304,307,310],{"class":34,"line":301},20,[32,303,275],{"class":42},[32,305,306],{"class":90},"100",[32,308,309],{"class":42},")    ",[32,311,312],{"class":56},"# len=   100  capacity≈   116  — ~16% over-allocated\n",[32,314,316,318,321,324],{"class":34,"line":315},21,[32,317,275],{"class":42},[32,319,320],{"class":90},"1000",[32,322,323],{"class":42},")   ",[32,325,326],{"class":56},"# len=  1000  capacity≈  1030  — ~3% over-allocated (ratio shrinks)\n",[32,328,330],{"class":34,"line":329},22,[32,331,50],{"emptyLinePlaceholder":49},[32,333,335],{"class":34,"line":334},23,[32,336,337],{"class":56},"# ── Tuple: no over-allocation (immutable, no growth needed) → smaller footprint ──\n",[32,339,341,344,346,349,352,355,358,360,363,365,368,370,373],{"class":34,"line":340},24,[32,342,343],{"class":42},"lst ",[32,345,110],{"class":38},[32,347,348],{"class":42}," [",[32,350,351],{"class":90},"1",[32,353,354],{"class":42},", ",[32,356,357],{"class":90},"2",[32,359,354],{"class":42},[32,361,362],{"class":90},"3",[32,364,354],{"class":42},[32,366,367],{"class":90},"4",[32,369,354],{"class":42},[32,371,372],{"class":90},"5",[32,374,375],{"class":42},"]\n",[32,377,379,382,384,387,389,391,393,395,397,399,401,403,405],{"class":34,"line":378},25,[32,380,381],{"class":42},"tup ",[32,383,110],{"class":38},[32,385,386],{"class":42}," (",[32,388,351],{"class":90},[32,390,354],{"class":42},[32,392,357],{"class":90},[32,394,354],{"class":42},[32,396,362],{"class":90},[32,398,354],{"class":42},[32,400,367],{"class":90},[32,402,354],{"class":42},[32,404,372],{"class":90},[32,406,264],{"class":42},[32,408,410,413,415,417,420,422,425,427,429,431],{"class":34,"line":409},26,[32,411,412],{"class":90},"print",[32,414,195],{"class":42},[32,416,198],{"class":38},[32,418,419],{"class":100},"\"list sizeof: ",[32,421,219],{"class":90},[32,423,424],{"class":42},"sys.getsizeof(lst)",[32,426,213],{"class":90},[32,428,261],{"class":100},[32,430,323],{"class":42},[32,432,433],{"class":56},"# ~120 bytes — header + 8 spare pointer slots\n",[32,435,437,439,441,443,446,448,451,453,455,458],{"class":34,"line":436},27,[32,438,412],{"class":90},[32,440,195],{"class":42},[32,442,198],{"class":38},[32,444,445],{"class":100},"\"tuple sizeof: ",[32,447,219],{"class":90},[32,449,450],{"class":42},"sys.getsizeof(tup)",[32,452,213],{"class":90},[32,454,261],{"class":100},[32,456,457],{"class":42},")  ",[32,459,460],{"class":56},"# ~64 bytes — header + exactly 5 slots, no spare\n",[32,462,464],{"class":34,"line":463},28,[32,465,50],{"emptyLinePlaceholder":49},[32,467,469],{"class":34,"line":468},29,[32,470,471],{"class":56},"# Tuples of constants can be folded into the code object's co_consts at compile time\n",[32,473,475],{"class":34,"line":474},30,[32,476,477],{"class":56},"# (a \"constant tuple\" optimization) — the tuple is created once, reused on each access\n",[19,479,480],{"language":21},[23,481,483],{"className":25,"code":482,"language":21,"meta":27,"style":27},"# ── Production: building a 2D matrix correctly ──\n# The #1 Python gotcha — shared inner references in list multiplication\n\n# ANTI-PATTERN: [[0] * cols] * rows — all rows are the SAME list object\ndef bad_matrix(rows, cols):\n    return [[0] * cols] * rows    # inner [0]*cols is created ONCE, referenced `rows` times\n\nboard = bad_matrix(3, 3)\nboard[0][0] = 1\nprint(board)   # [[1, 0, 0], [1, 0, 0], [1, 0, 0]] — ALL rows changed! same inner list object\n\n# CORRECT: comprehension creates a fresh inner list per row\ndef good_matrix(rows, cols):\n    return [[0] * cols for _ in range(rows)]   # [0]*cols evaluated fresh each iteration\n\nboard = good_matrix(3, 3)\nboard[0][0] = 1\nprint(board)   # [[1, 0, 0], [0, 0, 0], [0, 0, 0]] — only row 0 changed\n\n# ── Production: in-place filtering without allocation ──\n# Remove items matching a predicate WITHOUT creating a new list (memory-constrained environments)\ndef filter_inplace(lst: list, predicate):\n    \"\"\"Remove items where predicate(item) is True — O(n), no second allocation.\"\"\"\n    lst[:] = [x for x in lst if not predicate(x)]   # slice assignment mutates in place\n    # `lst[:] = ...` replaces the CONTENTS of lst, keeping the same object identity\n    # All external references to `lst` see the update (unlike `lst = [...]` which rebinds)\n\ndata = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nref = data   # external reference\nfilter_inplace(data, lambda x: x % 3 == 0)\nprint(data)   # [1, 2, 4, 5, 7, 8, 10] — filtered\nprint(ref is data)   # True — same object, ref sees the change too\n",[29,484,485,490,495,499,504,514,541,545,563,582,592,596,601,610,641,645,662,678,687,691,696,701,717,722,754,759,764,768,821,834,859,870],{"__ignoreMap":27},[32,486,487],{"class":34,"line":35},[32,488,489],{"class":56},"# ── Production: building a 2D matrix correctly ──\n",[32,491,492],{"class":34,"line":46},[32,493,494],{"class":56},"# The #1 Python gotcha — shared inner references in list multiplication\n",[32,496,497],{"class":34,"line":53},[32,498,50],{"emptyLinePlaceholder":49},[32,500,501],{"class":34,"line":60},[32,502,503],{"class":56},"# ANTI-PATTERN: [[0] * cols] * rows — all rows are the SAME list object\n",[32,505,506,508,511],{"class":34,"line":66},[32,507,80],{"class":38},[32,509,510],{"class":83}," bad_matrix",[32,512,513],{"class":42},"(rows, cols):\n",[32,515,516,519,522,524,527,530,533,535,538],{"class":34,"line":72},[32,517,518],{"class":38},"    return",[32,520,521],{"class":42}," [[",[32,523,278],{"class":90},[32,525,526],{"class":42},"] ",[32,528,529],{"class":38},"*",[32,531,532],{"class":42}," cols] ",[32,534,529],{"class":38},[32,536,537],{"class":42}," rows    ",[32,539,540],{"class":56},"# inner [0]*cols is created ONCE, referenced `rows` times\n",[32,542,543],{"class":34,"line":77},[32,544,50],{"emptyLinePlaceholder":49},[32,546,547,550,552,555,557,559,561],{"class":34,"line":97},[32,548,549],{"class":42},"board ",[32,551,110],{"class":38},[32,553,554],{"class":42}," bad_matrix(",[32,556,362],{"class":90},[32,558,354],{"class":42},[32,560,362],{"class":90},[32,562,264],{"class":42},[32,564,565,568,570,573,575,577,579],{"class":34,"line":104},[32,566,567],{"class":42},"board[",[32,569,278],{"class":90},[32,571,572],{"class":42},"][",[32,574,278],{"class":90},[32,576,526],{"class":42},[32,578,110],{"class":38},[32,580,581],{"class":90}," 1\n",[32,583,584,586,589],{"class":34,"line":116},[32,585,412],{"class":90},[32,587,588],{"class":42},"(board)   ",[32,590,591],{"class":56},"# [[1, 0, 0], [1, 0, 0], [1, 0, 0]] — ALL rows changed! same inner list object\n",[32,593,594],{"class":34,"line":134},[32,595,50],{"emptyLinePlaceholder":49},[32,597,598],{"class":34,"line":140},[32,599,600],{"class":56},"# CORRECT: comprehension creates a fresh inner list per row\n",[32,602,603,605,608],{"class":34,"line":146},[32,604,80],{"class":38},[32,606,607],{"class":83}," good_matrix",[32,609,513],{"class":42},[32,611,612,614,616,618,620,622,625,628,631,633,635,638],{"class":34,"line":152},[32,613,518],{"class":38},[32,615,521],{"class":42},[32,617,278],{"class":90},[32,619,526],{"class":42},[32,621,529],{"class":38},[32,623,624],{"class":42}," cols ",[32,626,627],{"class":38},"for",[32,629,630],{"class":42}," _ ",[32,632,125],{"class":38},[32,634,128],{"class":90},[32,636,637],{"class":42},"(rows)]   ",[32,639,640],{"class":56},"# [0]*cols evaluated fresh each iteration\n",[32,642,643],{"class":34,"line":163},[32,644,50],{"emptyLinePlaceholder":49},[32,646,647,649,651,654,656,658,660],{"class":34,"line":189},[32,648,549],{"class":42},[32,650,110],{"class":38},[32,652,653],{"class":42}," good_matrix(",[32,655,362],{"class":90},[32,657,354],{"class":42},[32,659,362],{"class":90},[32,661,264],{"class":42},[32,663,664,666,668,670,672,674,676],{"class":34,"line":267},[32,665,567],{"class":42},[32,667,278],{"class":90},[32,669,572],{"class":42},[32,671,278],{"class":90},[32,673,526],{"class":42},[32,675,110],{"class":38},[32,677,581],{"class":90},[32,679,680,682,684],{"class":34,"line":272},[32,681,412],{"class":90},[32,683,588],{"class":42},[32,685,686],{"class":56},"# [[1, 0, 0], [0, 0, 0], [0, 0, 0]] — only row 0 changed\n",[32,688,689],{"class":34,"line":287},[32,690,50],{"emptyLinePlaceholder":49},[32,692,693],{"class":34,"line":301},[32,694,695],{"class":56},"# ── Production: in-place filtering without allocation ──\n",[32,697,698],{"class":34,"line":315},[32,699,700],{"class":56},"# Remove items matching a predicate WITHOUT creating a new list (memory-constrained environments)\n",[32,702,703,705,708,711,714],{"class":34,"line":329},[32,704,80],{"class":38},[32,706,707],{"class":83}," filter_inplace",[32,709,710],{"class":42},"(lst: ",[32,712,713],{"class":90},"list",[32,715,716],{"class":42},", predicate):\n",[32,718,719],{"class":34,"line":334},[32,720,721],{"class":100},"    \"\"\"Remove items where predicate(item) is True — O(n), no second allocation.\"\"\"\n",[32,723,724,727,729,732,734,737,739,742,745,748,751],{"class":34,"line":340},[32,725,726],{"class":42},"    lst[:] ",[32,728,110],{"class":38},[32,730,731],{"class":42}," [x ",[32,733,627],{"class":38},[32,735,736],{"class":42}," x ",[32,738,125],{"class":38},[32,740,741],{"class":42}," lst ",[32,743,744],{"class":38},"if",[32,746,747],{"class":38}," not",[32,749,750],{"class":42}," predicate(x)]   ",[32,752,753],{"class":56},"# slice assignment mutates in place\n",[32,755,756],{"class":34,"line":378},[32,757,758],{"class":56},"    # `lst[:] = ...` replaces the CONTENTS of lst, keeping the same object identity\n",[32,760,761],{"class":34,"line":409},[32,762,763],{"class":56},"    # All external references to `lst` see the update (unlike `lst = [...]` which rebinds)\n",[32,765,766],{"class":34,"line":436},[32,767,50],{"emptyLinePlaceholder":49},[32,769,770,773,775,777,779,781,783,785,787,789,791,793,795,797,800,802,805,807,810,812,815,817,819],{"class":34,"line":463},[32,771,772],{"class":42},"data ",[32,774,110],{"class":38},[32,776,348],{"class":42},[32,778,351],{"class":90},[32,780,354],{"class":42},[32,782,357],{"class":90},[32,784,354],{"class":42},[32,786,362],{"class":90},[32,788,354],{"class":42},[32,790,367],{"class":90},[32,792,354],{"class":42},[32,794,372],{"class":90},[32,796,354],{"class":42},[32,798,799],{"class":90},"6",[32,801,354],{"class":42},[32,803,804],{"class":90},"7",[32,806,354],{"class":42},[32,808,809],{"class":90},"8",[32,811,354],{"class":42},[32,813,814],{"class":90},"9",[32,816,354],{"class":42},[32,818,292],{"class":90},[32,820,375],{"class":42},[32,822,823,826,828,831],{"class":34,"line":468},[32,824,825],{"class":42},"ref ",[32,827,110],{"class":38},[32,829,830],{"class":42}," data   ",[32,832,833],{"class":56},"# external reference\n",[32,835,836,839,842,845,848,851,854,857],{"class":34,"line":474},[32,837,838],{"class":42},"filter_inplace(data, ",[32,840,841],{"class":38},"lambda",[32,843,844],{"class":42}," x: x ",[32,846,847],{"class":38},"%",[32,849,850],{"class":90}," 3",[32,852,853],{"class":38}," ==",[32,855,856],{"class":90}," 0",[32,858,264],{"class":42},[32,860,862,864,867],{"class":34,"line":861},31,[32,863,412],{"class":90},[32,865,866],{"class":42},"(data)   ",[32,868,869],{"class":56},"# [1, 2, 4, 5, 7, 8, 10] — filtered\n",[32,871,873,875,878,881,884],{"class":34,"line":872},32,[32,874,412],{"class":90},[32,876,877],{"class":42},"(ref ",[32,879,880],{"class":38},"is",[32,882,883],{"class":42}," data)   ",[32,885,886],{"class":56},"# True — same object, ref sees the change too\n",[14,888,890],{"id":889},"slicing-deep-dive","Slicing Deep Dive",[19,892,893],{"language":21},[23,894,896],{"className":25,"code":895,"language":21,"meta":27,"style":27},"nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\nprint(nums[2:5])       # [2, 3, 4]\nprint(nums[:3])          # [0, 1, 2]\nprint(nums[7:])            # [7, 8, 9]\nprint(nums[-3:])              # [7, 8, 9]\nprint(nums[::2])                # [0, 2, 4, 6, 8]\nprint(nums[::-1])                 # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]  — reverse\nprint(nums[100:200])                # []  — never raises, clamps silently\n\n# Slices are shallow copies — a NEW list object\nsub = nums[2:5]\nsub.append(999)\nprint(nums)              # unaffected — [0, 1, ..., 9]\n",[29,897,898,947,951,971,986,1000,1015,1030,1046,1064,1068,1073,1091,1101],{"__ignoreMap":27},[32,899,900,903,905,907,909,911,913,915,917,919,921,923,925,927,929,931,933,935,937,939,941,943,945],{"class":34,"line":35},[32,901,902],{"class":42},"nums ",[32,904,110],{"class":38},[32,906,348],{"class":42},[32,908,278],{"class":90},[32,910,354],{"class":42},[32,912,351],{"class":90},[32,914,354],{"class":42},[32,916,357],{"class":90},[32,918,354],{"class":42},[32,920,362],{"class":90},[32,922,354],{"class":42},[32,924,367],{"class":90},[32,926,354],{"class":42},[32,928,372],{"class":90},[32,930,354],{"class":42},[32,932,799],{"class":90},[32,934,354],{"class":42},[32,936,804],{"class":90},[32,938,354],{"class":42},[32,940,809],{"class":90},[32,942,354],{"class":42},[32,944,814],{"class":90},[32,946,375],{"class":42},[32,948,949],{"class":34,"line":46},[32,950,50],{"emptyLinePlaceholder":49},[32,952,953,955,958,960,963,965,968],{"class":34,"line":53},[32,954,412],{"class":90},[32,956,957],{"class":42},"(nums[",[32,959,357],{"class":90},[32,961,962],{"class":42},":",[32,964,372],{"class":90},[32,966,967],{"class":42},"])       ",[32,969,970],{"class":56},"# [2, 3, 4]\n",[32,972,973,975,978,980,983],{"class":34,"line":60},[32,974,412],{"class":90},[32,976,977],{"class":42},"(nums[:",[32,979,362],{"class":90},[32,981,982],{"class":42},"])          ",[32,984,985],{"class":56},"# [0, 1, 2]\n",[32,987,988,990,992,994,997],{"class":34,"line":66},[32,989,412],{"class":90},[32,991,957],{"class":42},[32,993,804],{"class":90},[32,995,996],{"class":42},":])            ",[32,998,999],{"class":56},"# [7, 8, 9]\n",[32,1001,1002,1004,1006,1008,1010,1013],{"class":34,"line":72},[32,1003,412],{"class":90},[32,1005,957],{"class":42},[32,1007,174],{"class":38},[32,1009,362],{"class":90},[32,1011,1012],{"class":42},":])              ",[32,1014,999],{"class":56},[32,1016,1017,1019,1022,1024,1027],{"class":34,"line":77},[32,1018,412],{"class":90},[32,1020,1021],{"class":42},"(nums[::",[32,1023,357],{"class":90},[32,1025,1026],{"class":42},"])                ",[32,1028,1029],{"class":56},"# [0, 2, 4, 6, 8]\n",[32,1031,1032,1034,1036,1038,1040,1043],{"class":34,"line":97},[32,1033,412],{"class":90},[32,1035,1021],{"class":42},[32,1037,174],{"class":38},[32,1039,351],{"class":90},[32,1041,1042],{"class":42},"])                 ",[32,1044,1045],{"class":56},"# [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]  — reverse\n",[32,1047,1048,1050,1052,1054,1056,1059,1061],{"class":34,"line":104},[32,1049,412],{"class":90},[32,1051,957],{"class":42},[32,1053,306],{"class":90},[32,1055,962],{"class":42},[32,1057,1058],{"class":90},"200",[32,1060,1026],{"class":42},[32,1062,1063],{"class":56},"# []  — never raises, clamps silently\n",[32,1065,1066],{"class":34,"line":116},[32,1067,50],{"emptyLinePlaceholder":49},[32,1069,1070],{"class":34,"line":134},[32,1071,1072],{"class":56},"# Slices are shallow copies — a NEW list object\n",[32,1074,1075,1078,1080,1083,1085,1087,1089],{"class":34,"line":140},[32,1076,1077],{"class":42},"sub ",[32,1079,110],{"class":38},[32,1081,1082],{"class":42}," nums[",[32,1084,357],{"class":90},[32,1086,962],{"class":42},[32,1088,372],{"class":90},[32,1090,375],{"class":42},[32,1092,1093,1096,1099],{"class":34,"line":146},[32,1094,1095],{"class":42},"sub.append(",[32,1097,1098],{"class":90},"999",[32,1100,264],{"class":42},[32,1102,1103,1105,1108],{"class":34,"line":152},[32,1104,412],{"class":90},[32,1106,1107],{"class":42},"(nums)              ",[32,1109,1110],{"class":56},"# unaffected — [0, 1, ..., 9]\n",[1112,1113,1115],"h3",{"id":1114},"slice-assignment-replacing-inserting-deleting-via-slices","Slice assignment — replacing, inserting, deleting via slices",[19,1117,1118],{"language":21},[23,1119,1121],{"className":25,"code":1120,"language":21,"meta":27,"style":27},"nums = [0, 1, 2, 3, 4]\nnums[1:3] = [10, 20, 30]      # replace a slice with a differently-sized list\nprint(nums)                      # [0, 10, 20, 30, 3, 4]\n\nnums[1:4] = []                     # delete a range via empty-slice assignment\nprint(nums)                           # [0, 3, 4]\n\nnums[1:1] = [100, 200]                  # insert without replacing anything\nprint(nums)                                # [0, 100, 200, 3, 4]\n\ndel nums[0]                                  # del also works with slices\nprint(nums)                                     # [100, 200, 3, 4]\n",[29,1122,1123,1151,1186,1196,1200,1220,1230,1234,1262,1272,1276,1291],{"__ignoreMap":27},[32,1124,1125,1127,1129,1131,1133,1135,1137,1139,1141,1143,1145,1147,1149],{"class":34,"line":35},[32,1126,902],{"class":42},[32,1128,110],{"class":38},[32,1130,348],{"class":42},[32,1132,278],{"class":90},[32,1134,354],{"class":42},[32,1136,351],{"class":90},[32,1138,354],{"class":42},[32,1140,357],{"class":90},[32,1142,354],{"class":42},[32,1144,362],{"class":90},[32,1146,354],{"class":42},[32,1148,367],{"class":90},[32,1150,375],{"class":42},[32,1152,1153,1156,1158,1160,1162,1164,1166,1168,1170,1172,1175,1177,1180,1183],{"class":34,"line":46},[32,1154,1155],{"class":42},"nums[",[32,1157,351],{"class":90},[32,1159,962],{"class":42},[32,1161,362],{"class":90},[32,1163,526],{"class":42},[32,1165,110],{"class":38},[32,1167,348],{"class":42},[32,1169,292],{"class":90},[32,1171,354],{"class":42},[32,1173,1174],{"class":90},"20",[32,1176,354],{"class":42},[32,1178,1179],{"class":90},"30",[32,1181,1182],{"class":42},"]      ",[32,1184,1185],{"class":56},"# replace a slice with a differently-sized list\n",[32,1187,1188,1190,1193],{"class":34,"line":53},[32,1189,412],{"class":90},[32,1191,1192],{"class":42},"(nums)                      ",[32,1194,1195],{"class":56},"# [0, 10, 20, 30, 3, 4]\n",[32,1197,1198],{"class":34,"line":60},[32,1199,50],{"emptyLinePlaceholder":49},[32,1201,1202,1204,1206,1208,1210,1212,1214,1217],{"class":34,"line":66},[32,1203,1155],{"class":42},[32,1205,351],{"class":90},[32,1207,962],{"class":42},[32,1209,367],{"class":90},[32,1211,526],{"class":42},[32,1213,110],{"class":38},[32,1215,1216],{"class":42}," []                     ",[32,1218,1219],{"class":56},"# delete a range via empty-slice assignment\n",[32,1221,1222,1224,1227],{"class":34,"line":72},[32,1223,412],{"class":90},[32,1225,1226],{"class":42},"(nums)                           ",[32,1228,1229],{"class":56},"# [0, 3, 4]\n",[32,1231,1232],{"class":34,"line":77},[32,1233,50],{"emptyLinePlaceholder":49},[32,1235,1236,1238,1240,1242,1244,1246,1248,1250,1252,1254,1256,1259],{"class":34,"line":97},[32,1237,1155],{"class":42},[32,1239,351],{"class":90},[32,1241,962],{"class":42},[32,1243,351],{"class":90},[32,1245,526],{"class":42},[32,1247,110],{"class":38},[32,1249,348],{"class":42},[32,1251,306],{"class":90},[32,1253,354],{"class":42},[32,1255,1058],{"class":90},[32,1257,1258],{"class":42},"]                  ",[32,1260,1261],{"class":56},"# insert without replacing anything\n",[32,1263,1264,1266,1269],{"class":34,"line":104},[32,1265,412],{"class":90},[32,1267,1268],{"class":42},"(nums)                                ",[32,1270,1271],{"class":56},"# [0, 100, 200, 3, 4]\n",[32,1273,1274],{"class":34,"line":116},[32,1275,50],{"emptyLinePlaceholder":49},[32,1277,1278,1281,1283,1285,1288],{"class":34,"line":134},[32,1279,1280],{"class":38},"del",[32,1282,1082],{"class":42},[32,1284,278],{"class":90},[32,1286,1287],{"class":42},"]                                  ",[32,1289,1290],{"class":56},"# del also works with slices\n",[32,1292,1293,1295,1298],{"class":34,"line":140},[32,1294,412],{"class":90},[32,1296,1297],{"class":42},"(nums)                                     ",[32,1299,1300],{"class":56},"# [100, 200, 3, 4]\n",[14,1302,1304],{"id":1303},"list-comprehensions","List Comprehensions",[19,1306,1307],{"language":21},[23,1308,1310],{"className":25,"code":1309,"language":21,"meta":27,"style":27},"squares = [x ** 2 for x in range(10)]\nprint(squares)   # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n\nevens = [x for x in range(20) if x % 2 == 0]\nprint(evens)       # [0, 2, 4, ..., 18]\n\n# Nested comprehension — flatten a matrix\nmatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nflat = [num for row in matrix for num in row]\nprint(flat)   # [1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n# Conditional expression INSIDE the comprehension (ternary, not filter)\nlabels = [\"even\" if x % 2 == 0 else \"odd\" for x in range(5)]\nprint(labels)   # ['even', 'odd', 'even', 'odd', 'even']\n",[29,1311,1312,1343,1353,1357,1395,1405,1409,1414,1461,1491,1501,1505,1510,1555],{"__ignoreMap":27},[32,1313,1314,1317,1319,1321,1324,1327,1330,1332,1334,1336,1338,1340],{"class":34,"line":35},[32,1315,1316],{"class":42},"squares ",[32,1318,110],{"class":38},[32,1320,731],{"class":42},[32,1322,1323],{"class":38},"**",[32,1325,1326],{"class":90}," 2",[32,1328,1329],{"class":38}," for",[32,1331,736],{"class":42},[32,1333,125],{"class":38},[32,1335,128],{"class":90},[32,1337,195],{"class":42},[32,1339,292],{"class":90},[32,1341,1342],{"class":42},")]\n",[32,1344,1345,1347,1350],{"class":34,"line":46},[32,1346,412],{"class":90},[32,1348,1349],{"class":42},"(squares)   ",[32,1351,1352],{"class":56},"# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",[32,1354,1355],{"class":34,"line":53},[32,1356,50],{"emptyLinePlaceholder":49},[32,1358,1359,1362,1364,1366,1368,1370,1372,1374,1376,1378,1381,1383,1385,1387,1389,1391,1393],{"class":34,"line":60},[32,1360,1361],{"class":42},"evens ",[32,1363,110],{"class":38},[32,1365,731],{"class":42},[32,1367,627],{"class":38},[32,1369,736],{"class":42},[32,1371,125],{"class":38},[32,1373,128],{"class":90},[32,1375,195],{"class":42},[32,1377,1174],{"class":90},[32,1379,1380],{"class":42},") ",[32,1382,744],{"class":38},[32,1384,736],{"class":42},[32,1386,847],{"class":38},[32,1388,1326],{"class":90},[32,1390,853],{"class":38},[32,1392,856],{"class":90},[32,1394,375],{"class":42},[32,1396,1397,1399,1402],{"class":34,"line":66},[32,1398,412],{"class":90},[32,1400,1401],{"class":42},"(evens)       ",[32,1403,1404],{"class":56},"# [0, 2, 4, ..., 18]\n",[32,1406,1407],{"class":34,"line":72},[32,1408,50],{"emptyLinePlaceholder":49},[32,1410,1411],{"class":34,"line":77},[32,1412,1413],{"class":56},"# Nested comprehension — flatten a matrix\n",[32,1415,1416,1419,1421,1423,1425,1427,1429,1431,1433,1436,1438,1440,1442,1444,1446,1448,1450,1452,1454,1456,1458],{"class":34,"line":97},[32,1417,1418],{"class":42},"matrix ",[32,1420,110],{"class":38},[32,1422,521],{"class":42},[32,1424,351],{"class":90},[32,1426,354],{"class":42},[32,1428,357],{"class":90},[32,1430,354],{"class":42},[32,1432,362],{"class":90},[32,1434,1435],{"class":42},"], [",[32,1437,367],{"class":90},[32,1439,354],{"class":42},[32,1441,372],{"class":90},[32,1443,354],{"class":42},[32,1445,799],{"class":90},[32,1447,1435],{"class":42},[32,1449,804],{"class":90},[32,1451,354],{"class":42},[32,1453,809],{"class":90},[32,1455,354],{"class":42},[32,1457,814],{"class":90},[32,1459,1460],{"class":42},"]]\n",[32,1462,1463,1466,1468,1471,1473,1476,1478,1481,1483,1486,1488],{"class":34,"line":104},[32,1464,1465],{"class":42},"flat ",[32,1467,110],{"class":38},[32,1469,1470],{"class":42}," [num ",[32,1472,627],{"class":38},[32,1474,1475],{"class":42}," row ",[32,1477,125],{"class":38},[32,1479,1480],{"class":42}," matrix ",[32,1482,627],{"class":38},[32,1484,1485],{"class":42}," num ",[32,1487,125],{"class":38},[32,1489,1490],{"class":42}," row]\n",[32,1492,1493,1495,1498],{"class":34,"line":116},[32,1494,412],{"class":90},[32,1496,1497],{"class":42},"(flat)   ",[32,1499,1500],{"class":56},"# [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",[32,1502,1503],{"class":34,"line":134},[32,1504,50],{"emptyLinePlaceholder":49},[32,1506,1507],{"class":34,"line":140},[32,1508,1509],{"class":56},"# Conditional expression INSIDE the comprehension (ternary, not filter)\n",[32,1511,1512,1515,1517,1519,1522,1525,1527,1529,1531,1533,1535,1538,1541,1543,1545,1547,1549,1551,1553],{"class":34,"line":146},[32,1513,1514],{"class":42},"labels ",[32,1516,110],{"class":38},[32,1518,348],{"class":42},[32,1520,1521],{"class":100},"\"even\"",[32,1523,1524],{"class":38}," if",[32,1526,736],{"class":42},[32,1528,847],{"class":38},[32,1530,1326],{"class":90},[32,1532,853],{"class":38},[32,1534,856],{"class":90},[32,1536,1537],{"class":38}," else",[32,1539,1540],{"class":100}," \"odd\"",[32,1542,1329],{"class":38},[32,1544,736],{"class":42},[32,1546,125],{"class":38},[32,1548,128],{"class":90},[32,1550,195],{"class":42},[32,1552,372],{"class":90},[32,1554,1342],{"class":42},[32,1556,1557,1559,1562],{"class":34,"line":152},[32,1558,412],{"class":90},[32,1560,1561],{"class":42},"(labels)   ",[32,1563,1564],{"class":56},"# ['even', 'odd', 'even', 'odd', 'even']\n",[1566,1567,1568],"p",{},"Full comprehension coverage (set\u002Fdict\u002Fgenerator forms) continues in chapter 09 — this chapter focuses on list-specific mechanics.",[14,1570,1572],{"id":1571},"tuples-immutable-ordered-sequences","Tuples — Immutable, Ordered Sequences",[19,1574,1575],{"language":21},[23,1576,1578],{"className":25,"code":1577,"language":21,"meta":27,"style":27},"point = (3, 4)\nprint(point[0], point[1])   # 3 4\n\n# point[0] = 10   # TypeError: 'tuple' object does not support item assignment\n\n# Parentheses are optional — the COMMA makes it a tuple\nalso_a_tuple = 1, 2, 3\nprint(type(also_a_tuple))   # \u003Cclass 'tuple'>\n\nsingle = (42,)          # trailing comma REQUIRED for a 1-element tuple\nnot_a_tuple = (42)         # this is just an int in parentheses!\nprint(type(single), type(not_a_tuple))   # \u003Cclass 'tuple'> \u003Cclass 'int'>\n\nempty = ()\nprint(type(empty))   # \u003Cclass 'tuple'>\n",[29,1579,1580,1597,1617,1621,1626,1630,1635,1654,1669,1673,1691,1708,1727,1731,1741],{"__ignoreMap":27},[32,1581,1582,1585,1587,1589,1591,1593,1595],{"class":34,"line":35},[32,1583,1584],{"class":42},"point ",[32,1586,110],{"class":38},[32,1588,386],{"class":42},[32,1590,362],{"class":90},[32,1592,354],{"class":42},[32,1594,367],{"class":90},[32,1596,264],{"class":42},[32,1598,1599,1601,1604,1606,1609,1611,1614],{"class":34,"line":46},[32,1600,412],{"class":90},[32,1602,1603],{"class":42},"(point[",[32,1605,278],{"class":90},[32,1607,1608],{"class":42},"], point[",[32,1610,351],{"class":90},[32,1612,1613],{"class":42},"])   ",[32,1615,1616],{"class":56},"# 3 4\n",[32,1618,1619],{"class":34,"line":53},[32,1620,50],{"emptyLinePlaceholder":49},[32,1622,1623],{"class":34,"line":60},[32,1624,1625],{"class":56},"# point[0] = 10   # TypeError: 'tuple' object does not support item assignment\n",[32,1627,1628],{"class":34,"line":66},[32,1629,50],{"emptyLinePlaceholder":49},[32,1631,1632],{"class":34,"line":72},[32,1633,1634],{"class":56},"# Parentheses are optional — the COMMA makes it a tuple\n",[32,1636,1637,1640,1642,1645,1647,1649,1651],{"class":34,"line":77},[32,1638,1639],{"class":42},"also_a_tuple ",[32,1641,110],{"class":38},[32,1643,1644],{"class":90}," 1",[32,1646,354],{"class":42},[32,1648,357],{"class":90},[32,1650,354],{"class":42},[32,1652,1653],{"class":90},"3\n",[32,1655,1656,1658,1660,1663,1666],{"class":34,"line":97},[32,1657,412],{"class":90},[32,1659,195],{"class":42},[32,1661,1662],{"class":90},"type",[32,1664,1665],{"class":42},"(also_a_tuple))   ",[32,1667,1668],{"class":56},"# \u003Cclass 'tuple'>\n",[32,1670,1671],{"class":34,"line":104},[32,1672,50],{"emptyLinePlaceholder":49},[32,1674,1675,1678,1680,1682,1685,1688],{"class":34,"line":116},[32,1676,1677],{"class":42},"single ",[32,1679,110],{"class":38},[32,1681,386],{"class":42},[32,1683,1684],{"class":90},"42",[32,1686,1687],{"class":42},",)          ",[32,1689,1690],{"class":56},"# trailing comma REQUIRED for a 1-element tuple\n",[32,1692,1693,1696,1698,1700,1702,1705],{"class":34,"line":134},[32,1694,1695],{"class":42},"not_a_tuple ",[32,1697,110],{"class":38},[32,1699,386],{"class":42},[32,1701,1684],{"class":90},[32,1703,1704],{"class":42},")         ",[32,1706,1707],{"class":56},"# this is just an int in parentheses!\n",[32,1709,1710,1712,1714,1716,1719,1721,1724],{"class":34,"line":140},[32,1711,412],{"class":90},[32,1713,195],{"class":42},[32,1715,1662],{"class":90},[32,1717,1718],{"class":42},"(single), ",[32,1720,1662],{"class":90},[32,1722,1723],{"class":42},"(not_a_tuple))   ",[32,1725,1726],{"class":56},"# \u003Cclass 'tuple'> \u003Cclass 'int'>\n",[32,1728,1729],{"class":34,"line":146},[32,1730,50],{"emptyLinePlaceholder":49},[32,1732,1733,1736,1738],{"class":34,"line":152},[32,1734,1735],{"class":42},"empty ",[32,1737,110],{"class":38},[32,1739,1740],{"class":42}," ()\n",[32,1742,1743,1745,1747,1749,1752],{"class":34,"line":163},[32,1744,412],{"class":90},[32,1746,195],{"class":42},[32,1748,1662],{"class":90},[32,1750,1751],{"class":42},"(empty))   ",[32,1753,1668],{"class":56},[1112,1755,1757],{"id":1756},"tuple-packing-and-unpacking","Tuple Packing and Unpacking",[19,1759,1760],{"language":21},[23,1761,1763],{"className":25,"code":1762,"language":21,"meta":27,"style":27},"# Packing\ncoordinates = 10, 20, 30\n\n# Unpacking\nx, y, z = coordinates\nprint(x, y, z)   # 10 20 30\n\n# Swap without a temp variable — packs a tuple on the right, unpacks on the left\na, b = 1, 2\na, b = b, a\nprint(a, b)   # 2 1\n\n# Star-unpacking for \"the rest\"\nfirst, *middle, last = [1, 2, 3, 4, 5]\nprint(first, middle, last)   # 1 [2, 3, 4] 5\n\nfirst, second, *rest = range(10)\nprint(first, second, rest)   # 0 1 [2, 3, 4, 5, 6, 7, 8, 9]\n\n# Ignoring values with _\n_, _, third = (1, 2, 3)\nprint(third)   # 3\n",[29,1764,1765,1770,1789,1793,1798,1808,1818,1822,1827,1841,1850,1860,1864,1869,1903,1913,1917,1937,1947,1951,1956,1977],{"__ignoreMap":27},[32,1766,1767],{"class":34,"line":35},[32,1768,1769],{"class":56},"# Packing\n",[32,1771,1772,1775,1777,1780,1782,1784,1786],{"class":34,"line":46},[32,1773,1774],{"class":42},"coordinates ",[32,1776,110],{"class":38},[32,1778,1779],{"class":90}," 10",[32,1781,354],{"class":42},[32,1783,1174],{"class":90},[32,1785,354],{"class":42},[32,1787,1788],{"class":90},"30\n",[32,1790,1791],{"class":34,"line":53},[32,1792,50],{"emptyLinePlaceholder":49},[32,1794,1795],{"class":34,"line":60},[32,1796,1797],{"class":56},"# Unpacking\n",[32,1799,1800,1803,1805],{"class":34,"line":66},[32,1801,1802],{"class":42},"x, y, z ",[32,1804,110],{"class":38},[32,1806,1807],{"class":42}," coordinates\n",[32,1809,1810,1812,1815],{"class":34,"line":72},[32,1811,412],{"class":90},[32,1813,1814],{"class":42},"(x, y, z)   ",[32,1816,1817],{"class":56},"# 10 20 30\n",[32,1819,1820],{"class":34,"line":77},[32,1821,50],{"emptyLinePlaceholder":49},[32,1823,1824],{"class":34,"line":97},[32,1825,1826],{"class":56},"# Swap without a temp variable — packs a tuple on the right, unpacks on the left\n",[32,1828,1829,1832,1834,1836,1838],{"class":34,"line":104},[32,1830,1831],{"class":42},"a, b ",[32,1833,110],{"class":38},[32,1835,1644],{"class":90},[32,1837,354],{"class":42},[32,1839,1840],{"class":90},"2\n",[32,1842,1843,1845,1847],{"class":34,"line":116},[32,1844,1831],{"class":42},[32,1846,110],{"class":38},[32,1848,1849],{"class":42}," b, a\n",[32,1851,1852,1854,1857],{"class":34,"line":134},[32,1853,412],{"class":90},[32,1855,1856],{"class":42},"(a, b)   ",[32,1858,1859],{"class":56},"# 2 1\n",[32,1861,1862],{"class":34,"line":140},[32,1863,50],{"emptyLinePlaceholder":49},[32,1865,1866],{"class":34,"line":146},[32,1867,1868],{"class":56},"# Star-unpacking for \"the rest\"\n",[32,1870,1871,1874,1876,1879,1881,1883,1885,1887,1889,1891,1893,1895,1897,1899,1901],{"class":34,"line":152},[32,1872,1873],{"class":42},"first, ",[32,1875,529],{"class":38},[32,1877,1878],{"class":42},"middle, last ",[32,1880,110],{"class":38},[32,1882,348],{"class":42},[32,1884,351],{"class":90},[32,1886,354],{"class":42},[32,1888,357],{"class":90},[32,1890,354],{"class":42},[32,1892,362],{"class":90},[32,1894,354],{"class":42},[32,1896,367],{"class":90},[32,1898,354],{"class":42},[32,1900,372],{"class":90},[32,1902,375],{"class":42},[32,1904,1905,1907,1910],{"class":34,"line":163},[32,1906,412],{"class":90},[32,1908,1909],{"class":42},"(first, middle, last)   ",[32,1911,1912],{"class":56},"# 1 [2, 3, 4] 5\n",[32,1914,1915],{"class":34,"line":189},[32,1916,50],{"emptyLinePlaceholder":49},[32,1918,1919,1922,1924,1927,1929,1931,1933,1935],{"class":34,"line":267},[32,1920,1921],{"class":42},"first, second, ",[32,1923,529],{"class":38},[32,1925,1926],{"class":42},"rest ",[32,1928,110],{"class":38},[32,1930,128],{"class":90},[32,1932,195],{"class":42},[32,1934,292],{"class":90},[32,1936,264],{"class":42},[32,1938,1939,1941,1944],{"class":34,"line":272},[32,1940,412],{"class":90},[32,1942,1943],{"class":42},"(first, second, rest)   ",[32,1945,1946],{"class":56},"# 0 1 [2, 3, 4, 5, 6, 7, 8, 9]\n",[32,1948,1949],{"class":34,"line":287},[32,1950,50],{"emptyLinePlaceholder":49},[32,1952,1953],{"class":34,"line":301},[32,1954,1955],{"class":56},"# Ignoring values with _\n",[32,1957,1958,1961,1963,1965,1967,1969,1971,1973,1975],{"class":34,"line":315},[32,1959,1960],{"class":42},"_, _, third ",[32,1962,110],{"class":38},[32,1964,386],{"class":42},[32,1966,351],{"class":90},[32,1968,354],{"class":42},[32,1970,357],{"class":90},[32,1972,354],{"class":42},[32,1974,362],{"class":90},[32,1976,264],{"class":42},[32,1978,1979,1981,1984],{"class":34,"line":329},[32,1980,412],{"class":90},[32,1982,1983],{"class":42},"(third)   ",[32,1985,1986],{"class":56},"# 3\n",[1112,1988,1990],{"id":1989},"real-world-use-functions-returning-multiple-values-are-just-tuples","Real-world use: functions returning multiple values are just tuples",[19,1992,1993],{"language":21},[23,1994,1996],{"className":25,"code":1995,"language":21,"meta":27,"style":27},"def divide_with_remainder(a, b):\n    return a \u002F\u002F b, a % b     # implicitly packs a 2-tuple\n\nquotient, remainder = divide_with_remainder(17, 5)\nprint(quotient, remainder)   # 3 2\n",[29,1997,1998,2008,2028,2032,2051],{"__ignoreMap":27},[32,1999,2000,2002,2005],{"class":34,"line":35},[32,2001,80],{"class":38},[32,2003,2004],{"class":83}," divide_with_remainder",[32,2006,2007],{"class":42},"(a, b):\n",[32,2009,2010,2012,2015,2017,2020,2022,2025],{"class":34,"line":46},[32,2011,518],{"class":38},[32,2013,2014],{"class":42}," a ",[32,2016,180],{"class":38},[32,2018,2019],{"class":42}," b, a ",[32,2021,847],{"class":38},[32,2023,2024],{"class":42}," b     ",[32,2026,2027],{"class":56},"# implicitly packs a 2-tuple\n",[32,2029,2030],{"class":34,"line":53},[32,2031,50],{"emptyLinePlaceholder":49},[32,2033,2034,2037,2039,2042,2045,2047,2049],{"class":34,"line":60},[32,2035,2036],{"class":42},"quotient, remainder ",[32,2038,110],{"class":38},[32,2040,2041],{"class":42}," divide_with_remainder(",[32,2043,2044],{"class":90},"17",[32,2046,354],{"class":42},[32,2048,372],{"class":90},[32,2050,264],{"class":42},[32,2052,2053,2055,2058],{"class":34,"line":66},[32,2054,412],{"class":90},[32,2056,2057],{"class":42},"(quotient, remainder)   ",[32,2059,2060],{"class":56},"# 3 2\n",[14,2062,2064],{"id":2063},"why-tuples-exist-when-lists-can-do-the-same-thing","Why Tuples Exist When Lists Can Do the Same Thing",[2066,2067,2068,2076,2082],"ol",{},[2069,2070,2071,2075],"li",{},[2072,2073,2074],"strong",{},"Immutability signals intent"," — a tuple communicates \"this is a fixed record\u002Fcoordinate that should never change,\" which lists cannot express.",[2069,2077,2078,2081],{},[2072,2079,2080],{},"Hashability"," — tuples of hashable elements are themselves hashable, so they can be dict keys or set members; lists never can be.",[2069,2083,2084,2087],{},[2072,2085,2086],{},"Slight performance\u002Fmemory edge"," — tuples have a smaller fixed memory footprint than lists (no over-allocation for future growth) and can be marginally faster to construct\u002Fiterate.",[19,2089,2090],{"language":21},[23,2091,2093],{"className":25,"code":2092,"language":21,"meta":27,"style":27},"# Tuples as dict keys — impossible with lists\ndistances = {}\ndistances[(0, 0)] = 0\ndistances[(1, 1)] = 1.41\n\n# lst = [0, 0]\n# distances[lst] = 0   # TypeError: unhashable type: 'list'\n\nprint(distances[(1, 1)])   # 1.41\n",[29,2094,2095,2100,2110,2129,2146,2150,2155,2160,2164],{"__ignoreMap":27},[32,2096,2097],{"class":34,"line":35},[32,2098,2099],{"class":56},"# Tuples as dict keys — impossible with lists\n",[32,2101,2102,2105,2107],{"class":34,"line":46},[32,2103,2104],{"class":42},"distances ",[32,2106,110],{"class":38},[32,2108,2109],{"class":42}," {}\n",[32,2111,2112,2115,2117,2119,2121,2124,2126],{"class":34,"line":53},[32,2113,2114],{"class":42},"distances[(",[32,2116,278],{"class":90},[32,2118,354],{"class":42},[32,2120,278],{"class":90},[32,2122,2123],{"class":42},")] ",[32,2125,110],{"class":38},[32,2127,2128],{"class":90}," 0\n",[32,2130,2131,2133,2135,2137,2139,2141,2143],{"class":34,"line":60},[32,2132,2114],{"class":42},[32,2134,351],{"class":90},[32,2136,354],{"class":42},[32,2138,351],{"class":90},[32,2140,2123],{"class":42},[32,2142,110],{"class":38},[32,2144,2145],{"class":90}," 1.41\n",[32,2147,2148],{"class":34,"line":66},[32,2149,50],{"emptyLinePlaceholder":49},[32,2151,2152],{"class":34,"line":72},[32,2153,2154],{"class":56},"# lst = [0, 0]\n",[32,2156,2157],{"class":34,"line":77},[32,2158,2159],{"class":56},"# distances[lst] = 0   # TypeError: unhashable type: 'list'\n",[32,2161,2162],{"class":34,"line":97},[32,2163,50],{"emptyLinePlaceholder":49},[32,2165,2166,2168,2171,2173,2175,2177,2180],{"class":34,"line":104},[32,2167,412],{"class":90},[32,2169,2170],{"class":42},"(distances[(",[32,2172,351],{"class":90},[32,2174,354],{"class":42},[32,2176,351],{"class":90},[32,2178,2179],{"class":42},")])   ",[32,2181,2182],{"class":56},"# 1.41\n",[14,2184,2186],{"id":2185},"copying-shallow-vs-deep-the-source-of-countless-bugs","Copying — Shallow vs Deep, the Source of Countless Bugs",[19,2188,2189],{"language":21},[23,2190,2192],{"className":25,"code":2191,"language":21,"meta":27,"style":27},"import copy\n\noriginal = [[1, 2], [3, 4]]\n\n# Assignment is NOT a copy — same object\nalias = original\nalias[0].append(99)\nprint(original)   # [[1, 2, 99], [3, 4]] — original changed too, same object\n\n# Shallow copy — a NEW outer list, but inner lists are SHARED\nshallow = original.copy()          # or: list(original), or original[:]\nshallow.append([100])                 # adding to the OUTER list doesn't affect original\nprint(original)                          # unaffected by the .append([100]) above\n\nshallow[0].append(999)                     # but mutating a SHARED inner list DOES affect original\nprint(original)                               # [[1, 2, 99, 999], [3, 4]] — leaked through!\n\n# Deep copy — recursively copies EVERYTHING, no shared references at any depth\ndeep = copy.deepcopy(original)\ndeep[0].append(-1)\nprint(original)   # unaffected — [[1, 2, 99, 999], [3, 4]]\nprint(deep)          # [[1, 2, 99, 999, -1], [3, 4]]\n",[29,2193,2194,2201,2205,2230,2234,2239,2249,2264,2274,2278,2283,2296,2308,2318,2322,2339,2349,2353,2358,2368,2383,2392],{"__ignoreMap":27},[32,2195,2196,2198],{"class":34,"line":35},[32,2197,39],{"class":38},[32,2199,2200],{"class":42}," copy\n",[32,2202,2203],{"class":34,"line":46},[32,2204,50],{"emptyLinePlaceholder":49},[32,2206,2207,2210,2212,2214,2216,2218,2220,2222,2224,2226,2228],{"class":34,"line":53},[32,2208,2209],{"class":42},"original ",[32,2211,110],{"class":38},[32,2213,521],{"class":42},[32,2215,351],{"class":90},[32,2217,354],{"class":42},[32,2219,357],{"class":90},[32,2221,1435],{"class":42},[32,2223,362],{"class":90},[32,2225,354],{"class":42},[32,2227,367],{"class":90},[32,2229,1460],{"class":42},[32,2231,2232],{"class":34,"line":60},[32,2233,50],{"emptyLinePlaceholder":49},[32,2235,2236],{"class":34,"line":66},[32,2237,2238],{"class":56},"# Assignment is NOT a copy — same object\n",[32,2240,2241,2244,2246],{"class":34,"line":72},[32,2242,2243],{"class":42},"alias ",[32,2245,110],{"class":38},[32,2247,2248],{"class":42}," original\n",[32,2250,2251,2254,2256,2259,2262],{"class":34,"line":77},[32,2252,2253],{"class":42},"alias[",[32,2255,278],{"class":90},[32,2257,2258],{"class":42},"].append(",[32,2260,2261],{"class":90},"99",[32,2263,264],{"class":42},[32,2265,2266,2268,2271],{"class":34,"line":97},[32,2267,412],{"class":90},[32,2269,2270],{"class":42},"(original)   ",[32,2272,2273],{"class":56},"# [[1, 2, 99], [3, 4]] — original changed too, same object\n",[32,2275,2276],{"class":34,"line":104},[32,2277,50],{"emptyLinePlaceholder":49},[32,2279,2280],{"class":34,"line":116},[32,2281,2282],{"class":56},"# Shallow copy — a NEW outer list, but inner lists are SHARED\n",[32,2284,2285,2288,2290,2293],{"class":34,"line":134},[32,2286,2287],{"class":42},"shallow ",[32,2289,110],{"class":38},[32,2291,2292],{"class":42}," original.copy()          ",[32,2294,2295],{"class":56},"# or: list(original), or original[:]\n",[32,2297,2298,2301,2303,2305],{"class":34,"line":140},[32,2299,2300],{"class":42},"shallow.append([",[32,2302,306],{"class":90},[32,2304,1042],{"class":42},[32,2306,2307],{"class":56},"# adding to the OUTER list doesn't affect original\n",[32,2309,2310,2312,2315],{"class":34,"line":146},[32,2311,412],{"class":90},[32,2313,2314],{"class":42},"(original)                          ",[32,2316,2317],{"class":56},"# unaffected by the .append([100]) above\n",[32,2319,2320],{"class":34,"line":152},[32,2321,50],{"emptyLinePlaceholder":49},[32,2323,2324,2327,2329,2331,2333,2336],{"class":34,"line":163},[32,2325,2326],{"class":42},"shallow[",[32,2328,278],{"class":90},[32,2330,2258],{"class":42},[32,2332,1098],{"class":90},[32,2334,2335],{"class":42},")                     ",[32,2337,2338],{"class":56},"# but mutating a SHARED inner list DOES affect original\n",[32,2340,2341,2343,2346],{"class":34,"line":189},[32,2342,412],{"class":90},[32,2344,2345],{"class":42},"(original)                               ",[32,2347,2348],{"class":56},"# [[1, 2, 99, 999], [3, 4]] — leaked through!\n",[32,2350,2351],{"class":34,"line":267},[32,2352,50],{"emptyLinePlaceholder":49},[32,2354,2355],{"class":34,"line":272},[32,2356,2357],{"class":56},"# Deep copy — recursively copies EVERYTHING, no shared references at any depth\n",[32,2359,2360,2363,2365],{"class":34,"line":287},[32,2361,2362],{"class":42},"deep ",[32,2364,110],{"class":38},[32,2366,2367],{"class":42}," copy.deepcopy(original)\n",[32,2369,2370,2373,2375,2377,2379,2381],{"class":34,"line":301},[32,2371,2372],{"class":42},"deep[",[32,2374,278],{"class":90},[32,2376,2258],{"class":42},[32,2378,174],{"class":38},[32,2380,351],{"class":90},[32,2382,264],{"class":42},[32,2384,2385,2387,2389],{"class":34,"line":315},[32,2386,412],{"class":90},[32,2388,2270],{"class":42},[32,2390,2391],{"class":56},"# unaffected — [[1, 2, 99, 999], [3, 4]]\n",[32,2393,2394,2396,2399],{"class":34,"line":329},[32,2395,412],{"class":90},[32,2397,2398],{"class":42},"(deep)          ",[32,2400,2401],{"class":56},"# [[1, 2, 99, 999, -1], [3, 4]]\n",[1566,2403,2404,2407,2408,2411,2412,2415,2416,2419,2420,2424],{},[2072,2405,2406],{},"The mental model",": ",[29,2409,2410],{},".copy()"," (and ",[29,2413,2414],{},"list(x)",", and ",[29,2417,2418],{},"x[:]",") creates one new container, but its ",[2421,2422,2423],"em",{},"elements"," are the same objects as in the original — for a list of immutable elements (ints, strings, tuples) this is indistinguishable from a deep copy in practice, but for a list of mutable elements (lists, dicts, custom objects) it is a trap waiting to bite the first time someone mutates a nested element.",[19,2426,2427],{"language":21},[23,2428,2430],{"className":25,"code":2429,"language":21,"meta":27,"style":27},"# Shallow copy is FINE for flat lists of immutables\nflat = [1, 2, 3]\nshallow_flat = flat.copy()\nshallow_flat.append(4)\nprint(flat)   # [1, 2, 3] — no shared mutable elements to leak through\n\n# But dangerous for nested\u002Fmutable structures — always deepcopy those\nnested = [{\"count\": 0}, {\"count\": 0}]\nshallow_nested = nested.copy()\nshallow_nested[0][\"count\"] += 1\nprint(nested)   # [{'count': 1}, {'count': 0}] — leaked!\n",[29,2431,2432,2437,2457,2467,2476,2485,2489,2494,2523,2533,2551],{"__ignoreMap":27},[32,2433,2434],{"class":34,"line":35},[32,2435,2436],{"class":56},"# Shallow copy is FINE for flat lists of immutables\n",[32,2438,2439,2441,2443,2445,2447,2449,2451,2453,2455],{"class":34,"line":46},[32,2440,1465],{"class":42},[32,2442,110],{"class":38},[32,2444,348],{"class":42},[32,2446,351],{"class":90},[32,2448,354],{"class":42},[32,2450,357],{"class":90},[32,2452,354],{"class":42},[32,2454,362],{"class":90},[32,2456,375],{"class":42},[32,2458,2459,2462,2464],{"class":34,"line":53},[32,2460,2461],{"class":42},"shallow_flat ",[32,2463,110],{"class":38},[32,2465,2466],{"class":42}," flat.copy()\n",[32,2468,2469,2472,2474],{"class":34,"line":60},[32,2470,2471],{"class":42},"shallow_flat.append(",[32,2473,367],{"class":90},[32,2475,264],{"class":42},[32,2477,2478,2480,2482],{"class":34,"line":66},[32,2479,412],{"class":90},[32,2481,1497],{"class":42},[32,2483,2484],{"class":56},"# [1, 2, 3] — no shared mutable elements to leak through\n",[32,2486,2487],{"class":34,"line":72},[32,2488,50],{"emptyLinePlaceholder":49},[32,2490,2491],{"class":34,"line":77},[32,2492,2493],{"class":56},"# But dangerous for nested\u002Fmutable structures — always deepcopy those\n",[32,2495,2496,2499,2501,2504,2507,2509,2511,2514,2516,2518,2520],{"class":34,"line":97},[32,2497,2498],{"class":42},"nested ",[32,2500,110],{"class":38},[32,2502,2503],{"class":42}," [{",[32,2505,2506],{"class":100},"\"count\"",[32,2508,2407],{"class":42},[32,2510,278],{"class":90},[32,2512,2513],{"class":42},"}, {",[32,2515,2506],{"class":100},[32,2517,2407],{"class":42},[32,2519,278],{"class":90},[32,2521,2522],{"class":42},"}]\n",[32,2524,2525,2528,2530],{"class":34,"line":104},[32,2526,2527],{"class":42},"shallow_nested ",[32,2529,110],{"class":38},[32,2531,2532],{"class":42}," nested.copy()\n",[32,2534,2535,2538,2540,2542,2544,2546,2549],{"class":34,"line":116},[32,2536,2537],{"class":42},"shallow_nested[",[32,2539,278],{"class":90},[32,2541,572],{"class":42},[32,2543,2506],{"class":100},[32,2545,526],{"class":42},[32,2547,2548],{"class":38},"+=",[32,2550,581],{"class":90},[32,2552,2553,2555,2558],{"class":34,"line":134},[32,2554,412],{"class":90},[32,2556,2557],{"class":42},"(nested)   ",[32,2559,2560],{"class":56},"# [{'count': 1}, {'count': 0}] — leaked!\n",[14,2562,2564],{"id":2563},"common-listtuple-operations-and-their-complexity","Common List\u002FTuple Operations and Their Complexity",[2566,2567,2568,2587],"table",{},[2569,2570,2571],"thead",{},[2572,2573,2574,2578,2581,2584],"tr",{},[2575,2576,2577],"th",{},"Operation",[2575,2579,2580],{},"List",[2575,2582,2583],{},"Tuple",[2575,2585,2586],{},"Complexity",[2588,2589,2590,2607,2623,2639,2653,2670],"tbody",{},[2572,2591,2592,2599,2602,2604],{},[2593,2594,2595,2596],"td",{},"Index access ",[29,2597,2598],{},"x[i]",[2593,2600,2601],{},"Yes",[2593,2603,2601],{},[2593,2605,2606],{},"O(1)",[2572,2608,2609,2612,2617,2620],{},[2593,2610,2611],{},"Append",[2593,2613,2614],{},[29,2615,2616],{},".append()",[2593,2618,2619],{},"N\u002FA (immutable)",[2593,2621,2622],{},"O(1) amortized",[2572,2624,2625,2628,2633,2636],{},[2593,2626,2627],{},"Insert at front",[2593,2629,2630],{},[29,2631,2632],{},".insert(0, v)",[2593,2634,2635],{},"N\u002FA",[2593,2637,2638],{},"O(n) — shifts everything",[2572,2640,2641,2646,2648,2650],{},[2593,2642,2643,2644],{},"Membership ",[29,2645,125],{},[2593,2647,2601],{},[2593,2649,2601],{},[2593,2651,2652],{},"O(n) — linear scan",[2572,2654,2655,2658,2663,2667],{},[2593,2656,2657],{},"Concatenation",[2593,2659,2660],{},[29,2661,2662],{},"+",[2593,2664,2665],{},[29,2666,2662],{},[2593,2668,2669],{},"O(n + m)",[2572,2671,2672,2675,2680,2684],{},[2593,2673,2674],{},"Length",[2593,2676,2677],{},[29,2678,2679],{},"len()",[2593,2681,2682],{},[29,2683,2679],{},[2593,2685,2606],{},[1566,2687,2688,2691,2692,2695,2696,2699,2700,2703,2704,2707],{},[2072,2689,2690],{},"Best practice",": if you're repeatedly inserting\u002Fremoving from the ",[2421,2693,2694],{},"front"," of a list, use ",[29,2697,2698],{},"collections.deque"," instead — list's ",[29,2701,2702],{},".insert(0, ...)"," and ",[29,2705,2706],{},".pop(0)"," are O(n) because every remaining element must shift.",[19,2709,2710],{"language":21},[23,2711,2713],{"className":25,"code":2712,"language":21,"meta":27,"style":27},"from collections import deque\n\nqueue = deque()\nqueue.append(\"task1\")\nqueue.append(\"task2\")\nqueue.appendleft(\"urgent_task\")   # O(1), unlike list.insert(0, ...)\nprint(queue.popleft())              # 'urgent_task' — O(1), unlike list.pop(0)\n",[29,2714,2715,2728,2732,2742,2752,2761,2774],{"__ignoreMap":27},[32,2716,2717,2720,2723,2725],{"class":34,"line":35},[32,2718,2719],{"class":38},"from",[32,2721,2722],{"class":42}," collections ",[32,2724,39],{"class":38},[32,2726,2727],{"class":42}," deque\n",[32,2729,2730],{"class":34,"line":46},[32,2731,50],{"emptyLinePlaceholder":49},[32,2733,2734,2737,2739],{"class":34,"line":53},[32,2735,2736],{"class":42},"queue ",[32,2738,110],{"class":38},[32,2740,2741],{"class":42}," deque()\n",[32,2743,2744,2747,2750],{"class":34,"line":60},[32,2745,2746],{"class":42},"queue.append(",[32,2748,2749],{"class":100},"\"task1\"",[32,2751,264],{"class":42},[32,2753,2754,2756,2759],{"class":34,"line":66},[32,2755,2746],{"class":42},[32,2757,2758],{"class":100},"\"task2\"",[32,2760,264],{"class":42},[32,2762,2763,2766,2769,2771],{"class":34,"line":72},[32,2764,2765],{"class":42},"queue.appendleft(",[32,2767,2768],{"class":100},"\"urgent_task\"",[32,2770,323],{"class":42},[32,2772,2773],{"class":56},"# O(1), unlike list.insert(0, ...)\n",[32,2775,2776,2778,2781],{"class":34,"line":77},[32,2777,412],{"class":90},[32,2779,2780],{"class":42},"(queue.popleft())              ",[32,2782,2783],{"class":56},"# 'urgent_task' — O(1), unlike list.pop(0)\n",[14,2785,2787],{"id":2786},"tips-tricks","💡 Tips & Tricks",[2789,2790,2791,2813,2827,2843,2855],"ul",{},[2069,2792,2793,2805,2806,2809,2810,2812],{},[2072,2794,2795,2703,2798,2703,2801,2804],{},[29,2796,2797],{},"list[:]",[29,2799,2800],{},"list.copy()",[29,2802,2803],{},"list(other_list)"," are all equivalent shallow copies"," — pick whichever reads clearest in context; ",[29,2807,2808],{},"[:]"," is the most traditional\u002Fidiomatic but ",[29,2811,2410],{}," is more explicit for readers unfamiliar with slice tricks.",[2069,2814,2815,2818,2819,2822,2823,2826],{},[2072,2816,2817],{},"Unpack function returns directly instead of indexing"," — ",[29,2820,2821],{},"min_val, max_val = min(data), max(data)"," is clearer than separate lines, and ",[29,2824,2825],{},"first, *_, last = sequence"," grabs the first and last elements in one line without slicing twice.",[2069,2828,2829,2818,2835,2838,2839,2842],{},[2072,2830,2831,2834],{},[29,2832,2833],{},"list.sort(key=..., reverse=True)"," beats sorting then reversing",[29,2836,2837],{},"sorted(items, key=len, reverse=True)"," is both clearer and faster than ",[29,2840,2841],{},"sorted(items, key=len)[::-1]",", and importantly preserves stable-sort semantics correctly (reversing a stable sort's output does NOT give you a stable descending sort for equal keys).",[2069,2844,2845,2818,2851,2854],{},[2072,2846,2847,2850],{},[29,2848,2849],{},"bisect"," module for maintaining sorted lists efficiently",[29,2852,2853],{},"bisect.insort(sorted_list, value)"," inserts while maintaining order in O(n) (due to the shift) but with O(log n) search, far better than re-sorting after every insert.",[2069,2856,2857,2818,2863,2866,2867,2870],{},[2072,2858,2859,2860,2862],{},"Tuple unpacking in ",[29,2861,627],{}," loops replaces manual indexing",[29,2864,2865],{},"for key, value in items.items():"," beats ",[29,2868,2869],{},"for item in items.items(): key = item[0]; value = item[1]",".",[14,2872,2874],{"id":2873},"️-edge-cases-gotchas","⚠️ Edge Cases & Gotchas",[2789,2876,2877,2909,2928,2944,2975],{},[2069,2878,2879,2897,2898,2901,2902,2904,2905,2908],{},[2072,2880,2881,2884,2885,2884,2888,2884,2890,2893,2894],{},[29,2882,2883],{},".sort()","\u002F",[29,2886,2887],{},".reverse()",[29,2889,2616],{},[29,2891,2892],{},".extend()"," all return ",[29,2895,2896],{},"None"," — chaining or assigning their result (",[29,2899,2900],{},"x = my_list.sort()",") is a common and silent bug; these methods mutate and return ",[29,2903,2896],{}," by Python convention (distinguishing \"in-place mutation\" methods from \"returns new value\" methods\u002Ffunctions like ",[29,2906,2907],{},"sorted()",").",[2069,2910,2911,2818,2914,2917,2918,2920,2921,2923,2924,2927],{},[2072,2912,2913],{},"A single-element tuple requires a trailing comma — parentheses alone don't make a tuple",[29,2915,2916],{},"(42)"," is just ",[29,2919,91],{}," ",[29,2922,1684],{},"; ",[29,2925,2926],{},"(42,)"," is a tuple. This bites people writing what they think is a 1-tuple literal, especially in function calls expecting a tuple argument.",[2069,2929,2930,2818,2933,354,2935,2415,2937,2939,2940,2943],{},[2072,2931,2932],{},"Shallow copies share nested mutable objects — mutating a nested element leaks through the \"copy\"",[29,2934,2410],{},[29,2936,2414],{},[29,2938,2418],{}," are all shallow; use ",[29,2941,2942],{},"copy.deepcopy()"," for lists\u002Fdicts containing mutable elements when true independence is required.",[2069,2945,2946,2818,2952,2955,2956,2959,2960,2963,2964,2703,2967,2970,2971,2974],{},[2072,2947,2948,2949,2951],{},"List multiplication (",[29,2950,529],{},") with mutable elements replicates references, not independent objects",[29,2953,2954],{},"rows = [[0] * 3] * 3"," creates a list containing the ",[2421,2957,2958],{},"same"," inner list object three times, so ",[29,2961,2962],{},"rows[0][0] = 1"," changes ",[29,2965,2966],{},"rows[1][0]",[29,2968,2969],{},"rows[2][0]"," too. The fix: ",[29,2972,2973],{},"rows = [[0] * 3 for _ in range(3)]",", which creates a genuinely new inner list per iteration.",[2069,2976,2977,2986,2987,2989],{},[2072,2978,2979,2703,2982,2985],{},[29,2980,2981],{},"list.insert(0, x)",[29,2983,2984],{},"list.pop(0)"," are O(n), not O(1)"," — every other element must shift to make\u002Fclose the gap at the front; code that treats a list as a FIFO queue via these calls silently degrades to O(n²) for n operations. Use ",[29,2988,2698],{}," for queue-like access patterns.",[14,2991,2993],{"id":2992},"spot-the-bug","🧠 Spot the Bug",[1566,2995,2996],{},"A developer initializes a 2D game board and is confused why placing one piece affects every row.",[19,2998,2999],{"language":21},[23,3000,3002],{"className":25,"code":3001,"language":21,"meta":27,"style":27},"board = [[None] * 3] * 3\nboard[0][0] = \"X\"\nprint(board)\n",[29,3003,3004,3027,3044],{"__ignoreMap":27},[32,3005,3006,3008,3010,3012,3014,3016,3018,3020,3022,3024],{"class":34,"line":35},[32,3007,549],{"class":42},[32,3009,110],{"class":38},[32,3011,521],{"class":42},[32,3013,2896],{"class":90},[32,3015,526],{"class":42},[32,3017,529],{"class":38},[32,3019,850],{"class":90},[32,3021,526],{"class":42},[32,3023,529],{"class":38},[32,3025,3026],{"class":90}," 3\n",[32,3028,3029,3031,3033,3035,3037,3039,3041],{"class":34,"line":46},[32,3030,567],{"class":42},[32,3032,278],{"class":90},[32,3034,572],{"class":42},[32,3036,278],{"class":90},[32,3038,526],{"class":42},[32,3040,110],{"class":38},[32,3042,3043],{"class":100}," \"X\"\n",[32,3045,3046,3048],{"class":34,"line":53},[32,3047,412],{"class":90},[32,3049,3050],{"class":42},"(board)\n",[3052,3053,3054,3058,3102,3108,3145],"details",{},[3055,3056,3057],"summary",{},"Answer",[1566,3059,3060,3061,3064,3065,3068,3069,3072,3073,3076,3077,3080,3081,3084,3085,354,3088,2415,3091,3094,3095,3097,3098,3101],{},"Prints ",[29,3062,3063],{},"[['X', None, None], ['X', None, None], ['X', None, None]]"," — every row shows the ",[29,3066,3067],{},"\"X\"",", not just row 0. ",[29,3070,3071],{},"[None] * 3"," creates one list, ",[29,3074,3075],{},"[None, None, None]",". Then the outer ",[29,3078,3079],{},"* 3"," doesn't create three independent copies of that list — it creates a new outer list containing ",[2072,3082,3083],{},"three references to that exact same inner list object",". ",[29,3086,3087],{},"board[0]",[29,3089,3090],{},"board[1]",[29,3092,3093],{},"board[2]"," are literally ",[29,3096,880],{},"-identical. Assigning ",[29,3099,3100],{},"board[0][0] = \"X\""," mutates the one shared inner list, which is visible through all three outer references.",[1566,3103,3104,3105,3107],{},"The fix uses a list comprehension, which evaluates ",[29,3106,3071],{}," fresh on every iteration, producing three genuinely distinct list objects:",[19,3109,3110],{"language":21},[23,3111,3113],{"className":25,"code":3112,"language":21,"meta":27,"style":27},"board = [[None] * 3 for _ in range(3)]\n",[29,3114,3115],{"__ignoreMap":27},[32,3116,3117,3119,3121,3123,3125,3127,3129,3131,3133,3135,3137,3139,3141,3143],{"class":34,"line":35},[32,3118,549],{"class":42},[32,3120,110],{"class":38},[32,3122,521],{"class":42},[32,3124,2896],{"class":90},[32,3126,526],{"class":42},[32,3128,529],{"class":38},[32,3130,850],{"class":90},[32,3132,1329],{"class":38},[32,3134,630],{"class":42},[32,3136,125],{"class":38},[32,3138,128],{"class":90},[32,3140,195],{"class":42},[32,3142,362],{"class":90},[32,3144,1342],{"class":42},[1566,3146,3147,2407,3150,3152,3153,3156],{},[2072,3148,3149],{},"The lesson",[29,3151,529],{}," replication on a list of mutable objects (including nested lists) duplicates ",[2421,3154,3155],{},"references",", not the underlying objects — this is the same \"shared reference\" root cause as the mutable-default-argument bug and the shallow-copy bug, wearing a different disguise.",[14,3158,3160],{"id":3159},"key-takeaways","Key Takeaways",[2789,3162,3163,3179,3190,3202,3223],{},[2069,3164,3165,3166,2884,3169,2884,3172,3175,3176,3178],{},"Lists are mutable and support in-place ",[29,3167,3168],{},".append",[29,3170,3171],{},".sort",[29,3173,3174],{},".reverse"," (all returning ",[29,3177,2896],{}," by convention); tuples are immutable, hashable (if their contents are), and used for fixed records and dict\u002Fset keys.",[2069,3180,3181,3182,3185,3186,3189],{},"Slicing (",[29,3183,3184],{},"list[a:b:c]",") never raises ",[29,3187,3188],{},"IndexError",", always returns a new shallow-copied list, and supports slice assignment for replace\u002Finsert\u002Fdelete-in-place operations.",[2069,3191,3192,2884,3194,2884,3196,3198,3199,3201],{},[29,3193,2410],{},[29,3195,2414],{},[29,3197,2418],{}," are shallow — nested mutable elements are shared with the original; use ",[29,3200,2942],{}," when true independence at every level is required.",[2069,3203,3204,3207,3208,3211,3212,3214,3215,3218,3219,3222],{},[29,3205,3206],{},"[x] * n"," with a mutable ",[29,3209,3210],{},"x"," (especially a nested list) replicates references to the ",[2421,3213,2958],{}," object ",[29,3216,3217],{},"n"," times — use a comprehension (",[29,3220,3221],{},"[x_fresh for _ in range(n)]",") to get independent copies.",[2069,3224,3225,3226,3228,3229,354,3232,3235],{},"Use ",[29,3227,2698],{}," instead of a list for queue-like patterns (",[29,3230,3231],{},"insert(0, ...)",[29,3233,3234],{},"pop(0)",") — list operations at the front are O(n), deque's are O(1).",[3237,3238,3239],"style",{},"html pre.shiki code .svdQ7, html code.shiki .svdQ7{--shiki-default:#D73A49;--shiki-github-dark:#F97583}html pre.shiki code .ssxIu, html code.shiki .ssxIu{--shiki-default:#24292E;--shiki-github-dark:#E1E4E8}html pre.shiki code .sdCPZ, html code.shiki .sdCPZ{--shiki-default:#6A737D;--shiki-github-dark:#6A737D}html pre.shiki code .sIsaT, html code.shiki .sIsaT{--shiki-default:#6F42C1;--shiki-github-dark:#B392F0}html pre.shiki code .snvgF, html code.shiki .snvgF{--shiki-default:#005CC5;--shiki-github-dark:#79B8FF}html pre.shiki code .sJ6F3, html code.shiki .sJ6F3{--shiki-default:#032F62;--shiki-github-dark:#9ECBFF}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 .github-dark .shiki span {color: var(--shiki-github-dark);background: var(--shiki-github-dark-bg);font-style: var(--shiki-github-dark-font-style);font-weight: var(--shiki-github-dark-font-weight);text-decoration: var(--shiki-github-dark-text-decoration);}html.github-dark .shiki span {color: var(--shiki-github-dark);background: var(--shiki-github-dark-bg);font-style: var(--shiki-github-dark-font-style);font-weight: var(--shiki-github-dark-font-weight);text-decoration: var(--shiki-github-dark-text-decoration);}",{"title":27,"searchDepth":46,"depth":46,"links":3241},[3242,3243,3246,3247,3251,3252,3253,3254,3255,3256,3257],{"id":16,"depth":46,"text":17},{"id":889,"depth":46,"text":890,"children":3244},[3245],{"id":1114,"depth":53,"text":1115},{"id":1303,"depth":46,"text":1304},{"id":1571,"depth":46,"text":1572,"children":3248},[3249,3250],{"id":1756,"depth":53,"text":1757},{"id":1989,"depth":53,"text":1990},{"id":2063,"depth":46,"text":2064},{"id":2185,"depth":46,"text":2186},{"id":2563,"depth":46,"text":2564},{"id":2786,"depth":46,"text":2787},{"id":2873,"depth":46,"text":2874},{"id":2992,"depth":46,"text":2993},{"id":3159,"depth":46,"text":3160},"md",{},"\u002Fpython\u002F07-lists-and-tuples",{"title":5,"description":27},"python\u002F07-lists-and-tuples","EXcc053_t-yBmFTjQXDXgMi1_3cdVAOp2AnkMrLukgM",1789924651395]