{"id":"l-pda-1-3","courseId":"python-data-analysis","lang":"en","kind":"lesson","summary":"Hold many values in lists, look records up by key in dictionaries, and see how a list of dictionaries is already a small table.","html":"<p>One variable per value stops working the moment you have twelve months of revenue, or forty-eight products. Python’s two workhorse containers solve this: the <strong>list</strong>, for values in order, and the <strong>dictionary</strong>, for values you look up by name. Between them they cover almost every data shape you will meet before pandas takes over.</p>\n<h2 id=\"sec-1\" tabindex=\"-1\">Lists: values in order</h2>\n<p>Here is Cartwheel’s 2025 revenue by month, in dollars, January first:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"python\" data-run=\"python\" data-index=\"0\"><div class=\"code-head\"><span class=\"code-title\">Python</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"python\" data-index=\"0\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">monthly_2025 </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">16167</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">14906</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">18208</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">18817</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">17428</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">13933</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">,</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">                22968</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">32042</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">25514</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">30339</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">75334</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">41153</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">]</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">len</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(monthly_2025))   </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># 12 items</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(monthly_2025[</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">])     </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># January: positions start at 0</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(monthly_2025[</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">-</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">])    </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># December: negative positions count from the end</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">sum</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(monthly_2025), </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">max</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(monthly_2025), </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">min</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(monthly_2025))</span></span></code></pre><div class=\"run-output\" data-run-output=\"0\" hidden></div></div>\n<p>Positions (indexes) start at 0, so the twelfth month is at position 11. Asking for <code dir=\"ltr\">monthly_2025[12]</code> raises <code dir=\"ltr\">IndexError: list index out of range</code>.</p>\n<h3 id=\"sec-2\" tabindex=\"-1\">Slicing</h3>\n<p>A slice <code dir=\"ltr\">items[start:stop]</code> takes a run of items. The start is included and the stop is <strong>excluded</strong>, which feels odd for a day and then becomes convenient: <code dir=\"ltr\">[0:3]</code> is exactly three items.</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"python\" data-run=\"python\" data-index=\"1\"><div class=\"code-head\"><span class=\"code-title\">Python</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"python\" data-index=\"1\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">monthly_2025 </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">16167</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">14906</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">18208</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">18817</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">17428</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">13933</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">,</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">                22968</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">32042</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">25514</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">30339</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">75334</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">41153</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">]</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">q1 </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> monthly_2025[</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">:</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">3</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">]     </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># Jan, Feb, Mar</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">q4 </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> monthly_2025[</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">-</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">3</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">:]     </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># last three: Oct, Nov, Dec</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(q1, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">sum</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(q1))</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(q4, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">sum</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(q4))</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">sorted</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(monthly_2025)[:</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">3</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">])   </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># the three weakest months</span></span></code></pre><div class=\"run-output\" data-run-output=\"1\" hidden></div></div>\n<p>Q4 brought in 146,826 dollars against 49,281 in Q1: three times as much. That gap is the question this course will eventually answer, and a two-line slice was enough to see it.</p>\n<aside class=\"callout callout-mistake\" role=\"note\"><p class=\"callout-title\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3\"/><path d=\"M12 9v4\"/><path d=\"M12 17h.01\"/></svg><span><span class=\"callout-kind\">Common mistake</span><span class=\"callout-sep\" aria-hidden=\"true\"> · </span>Counting from 1</span></p>\n<p>April is <code dir=\"ltr\">monthly_2025[3]</code>, not <code dir=\"ltr\">[4]</code>. When a slice gives you a total that looks slightly off, check the boundaries first: <code dir=\"ltr\">[3:6]</code> means positions 3, 4 and 5. Print the slice itself, not only its sum, until you trust it.</p>\n</aside>\n<h3 id=\"sec-3\" tabindex=\"-1\">Changing a list</h3>\n<p>Lists are mutable: you can change them in place. <code dir=\"ltr\">append()</code> adds to the end, and assignment to a position replaces an item.</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"python\" data-run=\"python\" data-index=\"2\"><div class=\"code-head\"><span class=\"code-title\">Python</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"python\" data-index=\"2\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">months </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Jan\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Feb\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Mar\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">]</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">months.append(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Apr\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">)</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">months[</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">] </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"January\"</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(months, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Mar\"</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> in</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> months)</span></span></code></pre><div class=\"run-output\" data-run-output=\"2\" hidden></div></div>\n<p>The <code dir=\"ltr\">in</code> operator asks whether a value is present and returns <code dir=\"ltr\">True</code> or <code dir=\"ltr\">False</code>.</p>\n<p>Because lists are mutable, two names can share one list. <code dir=\"ltr\">backup = months</code> attaches a second name to the same list, so changing one changes “both”. When you really need an independent copy, write <code dir=\"ltr\">months.copy()</code>.</p>\n<h2 id=\"sec-4\" tabindex=\"-1\">Dictionaries: values you look up by key</h2>\n<p>A dictionary stores <strong>key: value</strong> pairs. Keys are usually strings; values can be anything.</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"python\" data-run=\"python\" data-index=\"3\"><div class=\"code-head\"><span class=\"code-title\">Python</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"python\" data-index=\"3\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">product </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> {</span></span>\n<span class=\"line\"><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">    \"product_id\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">7</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">,</span></span>\n<span class=\"line\"><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">    \"name\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Burr Coffee Grinder\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">,</span></span>\n<span class=\"line\"><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">    \"unit_price\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">129</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">,</span></span>\n<span class=\"line\"><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">    \"is_active\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">True</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">,</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(product[</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"name\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">])                 </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># look up by key</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">product[</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"unit_price\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">] </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 135.45</span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">         # update after the 5% price rise</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">product[</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"category\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">] </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"Coffee &#x26; Tea\"</span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">   # add a new key</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(product)</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(product.get(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"launched_on\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"unknown\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">))</span></span></code></pre><div class=\"run-output\" data-run-output=\"3\" hidden></div></div>\n<p><code dir=\"ltr\">product[&quot;launched_on&quot;]</code> would crash with <code dir=\"ltr\">KeyError: 'launched_on'</code>. <code dir=\"ltr\">.get()</code> returns a default instead. Use square brackets when a missing key is a bug you want to hear about, and <code dir=\"ltr\">.get()</code> when missing is normal.</p>\n<p>Three methods let you walk through a dictionary: <code dir=\"ltr\">.keys()</code>, <code dir=\"ltr\">.values()</code> and <code dir=\"ltr\">.items()</code> (pairs). You will use <code dir=\"ltr\">.items()</code> with loops in the next lesson.</p>\n<h2 id=\"sec-5\" tabindex=\"-1\">A list of dictionaries is a table</h2>\n<p>Put several dictionaries with the same keys into a list and you have rows and columns:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"python\" data-run=\"python\" data-index=\"4\"><div class=\"code-head\"><span class=\"code-title\">Python</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"python\" data-index=\"4\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">order_lines </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    {</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"order_id\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"product\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Foam Roller\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"quantity\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"unit_price\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">27.3</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">},</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    {</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"order_id\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1001</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"product\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Solar Path Lights (6)\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"quantity\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"unit_price\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">44.1</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">},</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    {</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"order_id\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1001</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"product\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Burr Coffee Grinder\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"quantity\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"unit_price\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">135.45</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">},</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">]</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">len</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(order_lines), </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"rows\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">)</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(order_lines[</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">][</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"product\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">])        </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># row 1, column \"product\"</span></span></code></pre><div class=\"run-output\" data-run-output=\"4\" hidden></div></div>\n<p><code dir=\"ltr\">order_lines[1][&quot;product&quot;]</code> reads like a spreadsheet cell reference: row first, then column.</p>\n<figure class=\"diagram\"><svg class=\"diagram-svg\" focusable=\"false\" viewBox=\"0 0 700 250\" role=\"img\" aria-labelledby=\"l-pda-1-3-t1\"><defs><marker id=\"arrow-l-pda-1-3-1\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path d=\"M0 0 L10 5 L0 10 z\" class=\"d-arrowhead\"/></marker></defs>\n  <title id=\"l-pda-1-3-t1\">On the left, a list holds three dictionaries, each with keys order_id, product and quantity. On the right, the same data drawn as a table: each dictionary becomes a row and each key becomes a column header.</title>\n  <text class=\"d-label-strong\" x=\"20\" y=\"28\">list of dicts</text>\n  <rect class=\"d-box\" x=\"20\" y=\"44\" width=\"280\" height=\"44\" rx=\"8\"/>\n  <text class=\"d-code\" x=\"32\" y=\"71\">{order_id: 1000, product: ..., qty: 2}</text>\n  <rect class=\"d-box\" x=\"20\" y=\"98\" width=\"280\" height=\"44\" rx=\"8\"/>\n  <text class=\"d-code\" x=\"32\" y=\"125\">{order_id: 1001, product: ..., qty: 2}</text>\n  <rect class=\"d-box\" x=\"20\" y=\"152\" width=\"280\" height=\"44\" rx=\"8\"/>\n  <text class=\"d-code\" x=\"32\" y=\"179\">{order_id: 1001, product: ..., qty: 2}</text>\n  <path class=\"d-arrow\" d=\"M310 120 L380 120\" marker-end=\"url(#arrow-l-pda-1-3-1)\"/>\n  <text class=\"d-label-strong\" x=\"400\" y=\"28\">table (DataFrame)</text>\n  <rect class=\"d-box-primary\" x=\"400\" y=\"44\" width=\"280\" height=\"36\" rx=\"6\"/>\n  <text class=\"d-label-strong\" x=\"420\" y=\"67\">order_id</text>\n  <text class=\"d-label-strong\" x=\"520\" y=\"67\">product</text>\n  <text class=\"d-label-strong\" x=\"620\" y=\"67\">qty</text>\n  <rect class=\"d-box\" x=\"400\" y=\"86\" width=\"280\" height=\"34\" rx=\"6\"/>\n  <text class=\"d-label\" x=\"420\" y=\"108\">1000</text>\n  <text class=\"d-label\" x=\"520\" y=\"108\">Foam Roller</text>\n  <text class=\"d-label\" x=\"630\" y=\"108\">2</text>\n  <rect class=\"d-box\" x=\"400\" y=\"126\" width=\"280\" height=\"34\" rx=\"6\"/>\n  <text class=\"d-label\" x=\"420\" y=\"148\">1001</text>\n  <text class=\"d-label\" x=\"520\" y=\"148\">Solar Lights</text>\n  <text class=\"d-label\" x=\"630\" y=\"148\">2</text>\n  <rect class=\"d-box\" x=\"400\" y=\"166\" width=\"280\" height=\"34\" rx=\"6\"/>\n  <text class=\"d-label\" x=\"420\" y=\"188\">1001</text>\n  <text class=\"d-label\" x=\"520\" y=\"188\">Grinder</text>\n  <text class=\"d-label\" x=\"630\" y=\"188\">2</text>\n  <text class=\"d-label-muted\" x=\"350\" y=\"232\" text-anchor=\"middle\">each dict is a row; each key is a column</text>\n</svg><figcaption>The same three order lines as a list of dictionaries and as a table</figcaption></figure>\n<h3 id=\"sec-6\" tabindex=\"-1\">The other table shape: a dictionary of lists</h3>\n<p>You can also store a table the other way round: one key per column, each holding a list of that column’s values.</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"python\" data-run=\"python\" data-index=\"5\"><div class=\"code-head\"><span class=\"code-title\">Python</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"python\" data-index=\"5\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">order_lines </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> {</span></span>\n<span class=\"line\"><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">    \"order_id\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1001</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1001</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">],</span></span>\n<span class=\"line\"><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">    \"product\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: [</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Foam Roller\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Solar Path Lights (6)\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Burr Coffee Grinder\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">],</span></span>\n<span class=\"line\"><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">    \"quantity\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">: [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">],</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(order_lines[</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"product\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">])      </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\"># a whole column at once</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">sum</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(order_lines[</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"quantity\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">]))</span></span></code></pre><div class=\"run-output\" data-run-output=\"5\" hidden></div></div>\n<p>Rows are easy to read in the list-of-dicts shape; columns are easy to total in the dict-of-lists shape. Both shapes show up in real life: APIs and JSON files usually hand you a list of records, while people building a small table by hand often write it column by column. pandas accepts both, and you will build your first DataFrame from a dictionary of lists in the next section.</p>\n<p>This is the bridge to pandas. A DataFrame is a far more capable version of this table: it can sum a whole column at once, filter rows by a condition and read a CSV in one call. Learning lists and dictionaries first means you will understand what pandas is doing for you, and you will recognise the list-of-dicts shape when an API or a JSON file hands you data.</p>\n<aside class=\"callout callout-tip\" role=\"note\"><p class=\"callout-title\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M15 14c.2-1 .7-1.7 1.5-2.5 1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5.7.7 1.3 1.5 1.5 2.5\"/><path d=\"M9 18h6\"/><path d=\"M10 22h4\"/></svg><span><span class=\"callout-kind\">Tip</span><span class=\"callout-sep\" aria-hidden=\"true\"> · </span>Which container?</span></p>\n<p>Use a list when order matters and you reach items by position (monthly totals, a sequence of steps). Use a dictionary when you reach items by name (a product’s price, a country’s code). If you catch yourself searching a list for a name, you wanted a dictionary.</p>\n</aside>\n<p>In the exercise you slice the monthly list and update a price dictionary. Next, loops and conditions let you process every item without writing one line per item.</p>\n","toc":[{"id":"sec-1","text":"Lists: values in order","level":2},{"id":"sec-2","text":"Slicing","level":3},{"id":"sec-3","text":"Changing a list","level":3},{"id":"sec-4","text":"Dictionaries: values you look up by key","level":2},{"id":"sec-5","text":"A list of dictionaries is a table","level":2},{"id":"sec-6","text":"The other table shape: a dictionary of lists","level":3}],"takeaways":["Lists are ordered and indexed from 0; <code dir=\"ltr\">items[-1]</code> is the last item and <code dir=\"ltr\">items[a:b]</code> stops before position <code dir=\"ltr\">b</code>.","<code dir=\"ltr\">sum()</code>, <code dir=\"ltr\">len()</code>, <code dir=\"ltr\">max()</code>, <code dir=\"ltr\">min()</code> and <code dir=\"ltr\">sorted()</code> work on any list of numbers and answer most quick questions.","A dictionary maps keys to values; use <code dir=\"ltr\">d[key]</code> when the key must exist and <code dir=\"ltr\">d.get(key, default)</code> when it might not.","A list of dictionaries with the same keys is a table, one dictionary per row, which is exactly what a DataFrame generalises."],"further":[{"title":"Data Structures - More on Lists","url":"https://docs.python.org/3/tutorial/datastructures.html#more-on-lists"},{"title":"Data Structures - Dictionaries","url":"https://docs.python.org/3/tutorial/datastructures.html#dictionaries"},{"title":"Built-in Types - Mapping Types (dict)","url":"https://docs.python.org/3/library/stdtypes.html#mapping-types-dict"}],"quiz":[{"q":"<p><code dir=\"ltr\">monthly</code> holds 12 monthly totals, January first. Which expression is the total for April, May and June?</p>\n","options":[{"text":"<code dir=\"ltr\">sum(monthly[4:6])</code>","why":"Position 4 is May (January is 0), and the slice stops before 6, so this adds May and June only."},{"text":"<code dir=\"ltr\">sum(monthly[3:6])</code>","why":"Correct. April is position 3, and the slice includes 3, 4 and 5, stopping before 6."},{"text":"<code dir=\"ltr\">sum(monthly[3:5])</code>","why":"The stop position is excluded, so this covers April and May only."},{"text":"<code dir=\"ltr\">sum(monthly[4:7])</code>","why":"This starts at May. Counting from 1 instead of 0 is the classic off-by-one slip."}],"answer":1},{"q":"<p><code dir=\"ltr\">prices = {&quot;Desk Mat XL&quot;: 29}</code>. What does <code dir=\"ltr\">prices.get(&quot;Monitor Arm&quot;, 0)</code> return?</p>","options":[{"text":"A <code dir=\"ltr\">KeyError</code>","why":"<code dir=\"ltr\">prices[&quot;Monitor Arm&quot;]</code> would raise <code dir=\"ltr\">KeyError</code>. <code dir=\"ltr\">.get()</code> exists precisely to avoid that."},{"text":"<code dir=\"ltr\">None</code>","why":"<code dir=\"ltr\">.get()</code> returns <code dir=\"ltr\">None</code> only when you don’t pass a default. Here the default is 0."},{"text":"<code dir=\"ltr\">0</code>","why":"Correct. The key is missing, so <code dir=\"ltr\">.get()</code> returns the default you supplied."}],"answer":2},{"q":"<p>What does this print?</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"python\" data-index=\"0\"><div class=\"code-head\"><span class=\"code-title\">Python</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">totals </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">65</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">18</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">33</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">]</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">backup </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> totals</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">totals.append(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">35</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">)</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">len</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(backup))</span></span></code></pre></div>\n","options":[{"text":"<code dir=\"ltr\">4</code>","why":"Correct. <code dir=\"ltr\">backup = totals</code> does not copy the list; both names point at the same list, so the appended value shows up through either name."},{"text":"<code dir=\"ltr\">3</code>","why":"That would be true if <code dir=\"ltr\">backup</code> were a copy, such as <code dir=\"ltr\">totals.copy()</code>. Plain assignment never copies."},{"text":"An error, because <code dir=\"ltr\">backup</code> was never appended to","why":"There is only one list here, with two names attached to it. <code dir=\"ltr\">len(backup)</code> works fine."}],"answer":0},{"q":"<p>You need the price of a product by name, thousands of times. Which structure fits best?</p>","options":[{"text":"A list of prices, searched with a loop each time","why":"It works, but every lookup scans the whole list, and you need a separate list to know which price belongs to which name."},{"text":"A dictionary with product names as keys and prices as values","why":"Correct. Dictionary lookup by key is direct and fast, and the name-to-price pairing is built in."},{"text":"A string containing all names and prices separated by commas","why":"You would have to split and search the text every time; strings are for text, not for lookups."}],"answer":1}],"exercise":{"kind":"python","files":[],"starter":"monthly_2025 = [16167, 14906, 18208, 18817, 17428, 13933,\n                22968, 32042, 25514, 30339, 75334, 41153]\nmonths = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\",\n          \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n\nprices = {\"Cast Iron Skillet 26cm\": 39, \"Burr Coffee Grinder\": 129, \"Cork Yoga Mat\": 72}\n\n# 1. Q4 total (October, November, December) using a slice\nq4_total = None\n\n# 2. Name of the month with the highest revenue, e.g. \"Mar\"\nbest_month = None\n\n# 3. Add \"Laptop Stand\" to prices at 49\n\n# 4. The grinder's price after a 5% rise, rounded to 2 decimals\ngrinder_new_price = None\n","solution":"monthly_2025 = [16167, 14906, 18208, 18817, 17428, 13933,\n                22968, 32042, 25514, 30339, 75334, 41153]\nmonths = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\",\n          \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n\nprices = {\"Cast Iron Skillet 26cm\": 39, \"Burr Coffee Grinder\": 129, \"Cork Yoga Mat\": 72}\n\nq4_total = sum(monthly_2025[9:12])\n\nbest_position = monthly_2025.index(max(monthly_2025))\nbest_month = months[best_position]\n\nprices[\"Laptop Stand\"] = 49\n\ngrinder_new_price = round(prices[\"Burr Coffee Grinder\"] * 1.05, 2)\n","checks":[{"id":"q4-total","test":"q4_total == 146826"},{"id":"best-month","test":"best_month == 'Nov'"},{"id":"laptop-stand","test":"prices.get('Laptop Stand') == 49 and len(prices) == 4"},{"id":"grinder-price","test":"isinstance(grinder_new_price, float) and grinder_new_price == 135.45"},{"id":"lists-untouched","test":"len(monthly_2025) == 12 and months[10] == 'Nov'"}],"title":"Slice the year and update prices","prompt":"<p>Using the <code dir=\"ltr\">monthly_2025</code> and <code dir=\"ltr\">months</code> lists and the <code dir=\"ltr\">prices</code> dictionary: set <code dir=\"ltr\">q4_total</code> to the sum of October to December using a slice; set <code dir=\"ltr\">best_month</code> to the three-letter name of the month with the highest revenue; add <code dir=\"ltr\">&quot;Laptop Stand&quot;</code> to <code dir=\"ltr\">prices</code> at 49; and set <code dir=\"ltr\">grinder_new_price</code> to the Burr Coffee Grinder’s price after a 5% rise, rounded to 2 decimals. Work it out with code rather than typing the numbers.</p>\n","hints":["October is position 9 because January is 0. A slice that ends at 12 (or <code dir=\"ltr\">[-3:]</code>) includes December.","<code dir=\"ltr\">max(monthly_2025)</code> gives the highest value; <code dir=\"ltr\">monthly_2025.index(value)</code> gives its position; the same position in <code dir=\"ltr\">months</code> gives its name.","Add a key with <code dir=\"ltr\">prices[&quot;Laptop Stand&quot;] = 49</code>. For the rise, <code dir=\"ltr\">round(prices[&quot;Burr Coffee Grinder&quot;] * 1.05, 2)</code>."],"explanation":"<p><code dir=\"ltr\">monthly_2025[9:12]</code> (or <code dir=\"ltr\">[-3:]</code>) picks positions 9, 10 and 11, which are October to December. The best month takes two steps: find the largest value, then find where it sits with <code dir=\"ltr\">.index()</code>, and use that position in the parallel <code dir=\"ltr\">months</code> list. Two lists kept in the same order are fragile, which is one reason a table with labelled rows (a pandas Series) is nicer, as you will see in Section 2. Assigning to a new key adds it to the dictionary, and <code dir=\"ltr\">129 * 1.05</code> rounds to 135.45, the grinder’s real price after Cartwheel’s March 2025 rise.</p>\n","checkLabels":{"q4-total":"<code dir=\"ltr\">q4_total</code> is 146,826","best-month":"<code dir=\"ltr\">best_month</code> is <code dir=\"ltr\">&quot;Nov&quot;</code>","laptop-stand":"<code dir=\"ltr\">prices</code> now has 4 products, including Laptop Stand at 49","grinder-price":"<code dir=\"ltr\">grinder_new_price</code> is 135.45","lists-untouched":"The original lists still have all 12 months"}},"words":664,"readMinutes":3}