<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[The Python Coding Stack: The Club]]></title><description><![CDATA[The Club is the area for premium subscribers with more exclusive Python articles and perspectives, perks, a forum, and more.]]></description><link>https://www.thepythoncodingstack.com/s/the-club</link><image><url>https://substackcdn.com/image/fetch/$s_!Dn3k!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab4a59e8-e362-456b-8427-934e87c31a0d_600x600.png</url><title>The Python Coding Stack: The Club</title><link>https://www.thepythoncodingstack.com/s/the-club</link></image><generator>Substack</generator><lastBuildDate>Wed, 22 Apr 2026 08:39:12 GMT</lastBuildDate><atom:link href="https://www.thepythoncodingstack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Stephen Gruppetta]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[thepythoncodingstack@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[thepythoncodingstack@substack.com]]></itunes:email><itunes:name><![CDATA[Stephen Gruppetta]]></itunes:name></itunes:owner><itunes:author><![CDATA[Stephen Gruppetta]]></itunes:author><googleplay:owner><![CDATA[thepythoncodingstack@substack.com]]></googleplay:owner><googleplay:email><![CDATA[thepythoncodingstack@substack.com]]></googleplay:email><googleplay:author><![CDATA[Stephen Gruppetta]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Field Notes: First, Second, and Five Hundred and Twenty-Third • [Club]]]></title><description><![CDATA[Have you ever needed to convert numbers like 22 into the string &#8220;twenty-two&#8221; or 523 into &#8220;five hundred and twenty-third&#8221;?]]></description><link>https://www.thepythoncodingstack.com/p/field-notes-first-second-and-five</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/field-notes-first-second-and-five</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Mon, 09 Mar 2026 15:25:48 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/4b146607-5d93-4c07-aed6-8b65fce4e239_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><em>Most of my posts on</em> The Python Coding Stack<em>, whether in the main publication or here in</em> The Club<em>, typically focus on some aspect of core Python and explore it through a step-by-step approach, a mini-project, or sometimes through an essay-type article.</em></p><p><em>But today, I&#8217;ll write a short post about some tools I came across that may be interesting. I&#8217;ll keep the post short since, if you&#8217;re interested, you can easily explore the packages independently &#8211; you won&#8217;t need my explanatory efforts.</em></p><div><hr></div><p>Remember some of the earliest code you ever wrote? It may have looked like this:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;59aefe4d-d092-4ca5-8e83-abda31f26c1a&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">first = input(&#8221;Enter the first number: &#8220;)
second = input(&#8221;Enter the second number: &#8220;)
print(
    f&#8221;The sum of the two numbers is {float(first) + float(second)}&#8221;
)</code></pre></div><p>Those days are long gone. But what if you wanted this code to work for any number of inputs, not just two?</p><p>I&#8217;m not talking about the code to work out the sum itself. You&#8217;d probably use a list to collect all the numbers and the <code>sum()</code> built-in function, or just use a running total. Whatever.</p><p>The annoying part is making user-friendly strings when asking for the input &#8211; it&#8217;s fine to write <code>"first"</code> and <code>"second"</code> when there are only two prompts &#8211; and then when showing the result, which currently says <code>"two numbers"</code>. But what if you have more?</p><p>I came across this need several times, but my solution has always been to change the string so that it&#8217;s general enough to work in all cases. I&#8217;m lazy, I know.</p><p>In any case, recently I decided to look for solutions. And of course, they exist! Everything seems to exist in the Python ecosystem. Several solutions&#8230;</p><h2><code>num2words</code></h2><p>The first solution is probably the simplest: the <code>num2words</code> package doesn&#8217;t do much beyond converting numbers to words, as its name suggests. You&#8217;ll need to install <code>num2words</code> using <code>pip</code>, <code>uv</code>, or whatever you use to install packages.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;5dc57e66-4a9b-46c9-bc1d-f2bac6e8a001&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">&gt;&gt;&gt; import num2words
&gt;&gt;&gt; num2words.num2words(34)
&#8216;thirty-four&#8217;</code></pre></div><p>The main function in the <code>num2words</code> module is also called <code>num2words()</code>, as often happens with such modules!</p><p>How far can it go?</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;6e551eb0-1a02-451d-852b-9cdcc1cb5ab6&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">&gt;&gt;&gt; num2words.num2words(5468.23)
&#8216;five thousand, four hundred and sixty-eight point two three&#8217;</code></pre></div><p>Working in Spanish?</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;a36e2039-283d-4493-9031-fd0575d6a10a&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">&gt;&gt;&gt; num2words.num2words(5468.23, lang=&#8221;es&#8221;)
&#8216;cinco mil cuatrocientos sesenta y ocho punto dos tres&#8217;</code></pre></div><p>Or Japanese?</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;f65faf64-470e-44cd-b50d-f536948c1b85&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">&gt;&gt;&gt; num2words.num2words(5468.23, lang=&#8221;ja&#8221;)
&#8216;&#20116;&#21315;&#22235;&#30334;&#20845;&#21313;&#20843;&#28857;&#20108;&#19977;&#8217;</code></pre></div>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/field-notes-first-second-and-five">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[The Journey From LBYL to EAFP • [Club]]]></title><description><![CDATA[When it&#8217;s OK to just go for it and see what happens]]></description><link>https://www.thepythoncodingstack.com/p/the-journey-from-lbyl-to-eafp</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/the-journey-from-lbyl-to-eafp</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Thu, 19 Feb 2026 22:26:35 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/8c79170f-29bd-411d-9b29-14960988ca33_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>LBYL came more naturally to me in my early years of programming. It seemed to have fewer obstacles in those early stages, fewer tricky concepts.</p><p>And in my 10+ years of teaching Python, I also preferred teaching LBYL to beginners and delaying EAFP until later.</p><p>But over the years, as I came to understand Python&#8217;s psyche better, I gradually shifted my programming style&#8212;and then, my teaching style, too.</p><p>So, what are LBYL and EAFP? And which one is more suited to Python?</p><div class="pullquote"><p><em>I&#8217;m running a series of three live workshops starting this week.<br>Each workshop is 2 hours long, so plenty to time to explore core Python topics:</em></p><p><em>#1 &#8226; Python&#8217;s Plumbing: Dunder Methods and Python&#8217;s Hidden Interface<br>#2 &#8226; Pythonic Iteration: Iterables, Iterators, </em><code>itertools<br></code><em>#3 &#8226; To Inherit or Not? Inheritance, Composition, Abstract Base Classes, and Protocols</em></p><p><em>Read more and book your place here:<br><a href="https://www.thepythoncodingstack.com/p/when-it-works-is-not-good-enough">https://www.thepythoncodingstack.com/p/when-it-works-is-not-good-enough</a></em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.thepythoncodingstack.com/p/when-it-works-is-not-good-enough&quot;,&quot;text&quot;:&quot;Book Workshops&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.thepythoncodingstack.com/p/when-it-works-is-not-good-enough"><span>Book Workshops</span></a></p></div><h2><strong>Look Both Sides Before Crossing the Road</strong></h2><p>You should definitely look before you leap across a busy road&#8230;or any road, really. And programming also has a <em>Look Before You Leap</em> concept&#8212;that&#8217;s LBYL&#8212;when handling potential failure points in your code.</p><p>Let&#8217;s start by considering this basic example. You define a function that accepts a value and a list. The function adds the value to the list if the value is above a user-supplied threshold:</p><pre><code>def add_value_above_threshold(value, threshold, data):
    if value &gt;= threshold:
        data.append(value)</code></pre><p>You can confirm this short code works as intended:</p><pre><code># ...
prices = []
add_value_above_threshold(12, 5, prices)
add_value_above_threshold(3, 5, prices)
add_value_above_threshold(9, 5, prices)
print(prices)</code></pre><p>This code outputs the list with the two prices above the threshold:</p><pre><code>[12, 9]</code></pre><p>However, you want to ensure this can&#8217;t happen:</p><pre><code># ...
products = {}
add_value_above_threshold(12, 5, products)</code></pre><p>Now, <code>products</code> is a dictionary, but <code>add_value_above_threshold()</code> was designed to work with lists and not dictionaries:</p><pre><code>Traceback (most recent call last):
  ...
    add_value_above_threshold(12, 5, products)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  ...
    data.append(value)
    ^^^^^^^^^^^
AttributeError: &#8216;dict&#8217; object has no attribute &#8216;append&#8217;</code></pre><p>One option is the look before you leap (LBYL):</p><pre><code>def add_value_above_threshold(value, threshold, data):
    if not isinstance(data, list):
        print(&#8221;Invalid format. &#8216;data&#8217; must be a list&#8221;)
        return
    if value &gt;= threshold:
        data.append(value)</code></pre><p>Now, the function prints a warning when you pass a dictionary, and it doesn&#8217;t crash the program!</p><p>But this is too restrictive.</p><p>Let&#8217;s assume you decide to use a <a href="https://www.thepythoncodingstack.com/p/clearing-the-deque-python-linked-list">deque</a> instead of a list:</p><pre><code>from collections import deque
&#8203;
# ...
&#8203;
prices = deque()
add_value_above_threshold(12, 5, prices)
add_value_above_threshold(3, 5, prices)
add_value_above_threshold(9, 5, prices)
print(prices)</code></pre><p>This code still complains that it wants a list and doesn&#8217;t play ball:</p><pre><code>Invalid format. &#8216;data&#8217; must be a list
Invalid format. &#8216;data&#8217; must be a list
Invalid format. &#8216;data&#8217; must be a list
deque([])</code></pre><p>But there&#8217;s no reason why this code shouldn&#8217;t work since <code>deque</code> also has an <code>.append()</code> method.</p><p>You could change the call to <code>isinstance()</code> to include the <code>deque</code> data type&#8212;<code>isinstance(data, list | deque)</code>&#8212;but then there may be other data structures that are valid and can be used in this function. You don&#8217;t want to have to write them all.</p><p>If you&#8217;re well-versed with the categories of data structures&#8212;perhaps because you devoured the <a href="https://www.thepythoncodingstack.com/p/the-main-data-structure-categories">The Python Data Structure Categories Series</a>&#8212;then you might conclude you need to check whether the object is a <code>MutableSequence</code> since all mutable sequences have an <code>.append()</code> method. You can import <code>MutableSequence</code> from <code>collections.abc</code> and use <code>isinstance(data, MutableSequence)</code>. Now you&#8217;re fine to use lists, deques, or any other mutable sequence.</p><p>This version fits better with Python&#8217;s duck-typing philosophy. It doesn&#8217;t restrict the function to a limited number of data types but to a category of data types. This category is defined by what the data types can do. In duck typing, you care about what an object <em>can do</em> rather than what <em>it is.</em> You can read more about duck typing in Python in this post: <a href="https://www.thepythoncodingstack.com/p/python-duck-typing-functions-classes-callables">When a Duck Calls Out &#8226; On Duck Typing and Callables in Python</a></p><p>However, you could still have other data types that have an <code>.append()</code> method but may not fully fit into the <code>MutableSequence</code> category. There&#8217;s no reason you should exclude those data types from working with your function.</p><p>Perhaps, you could use Python&#8217;s built-in <code>hasattr()</code> to check whether the object you pass has an <code>.append()</code> attribute. You&#8217;re now checking whether the object has the required attribute rather than what the object is.</p><p>But if you&#8217;re going through all this trouble, you can go a step further.</p><h2><strong>Just Go For It and See What Happens</strong></h2><p>Why not just run the line of code that includes <code>data.append()</code> and see what happens? Ah, but you don&#8217;t want the code to fail if you use the wrong data type&#8212;you only want to print a warning, say.</p><p>That&#8217;s where the <code>try..except</code> construct comes in:</p><pre><code>def add_value_above_threshold(value, threshold, data):
    if value &lt; threshold:  # inequality flipped to avoid nesting
        return
    try:
        data.append(value)
    except AttributeError:
        print(
            &#8220;Provided data structure does not support appending values.&#8221;
        )</code></pre><p>This is the <em>Easier to Ask for Forgiveness than Permission</em> (EAFP) philosophy. Just try the code. If it doesn&#8217;t work, you can then deal with it in the <code>except</code> block. Now, this fits even more nicely with Python&#8217;s duck typing philosophy. You&#8217;re asking the program whether <code>data</code> can append a value. It doesn&#8217;t matter what <code>data</code> is&#8211;can it append a value?</p><p>You don&#8217;t have to think about all the valid data types or which category they fall into. And rather than checking whether the data type has the <code>.append()</code> attribute first, you just try to run the code and deal with the consequences later. That&#8217;s why it&#8217;s easier to ask for forgiveness than permission.</p><p>But don&#8217;t use this philosophy when crossing a busy road. Stick with &#8220;look before you leap&#8221; there!</p><h2><strong>Another Example Comparing LBYL and EAFP</strong></h2>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/the-journey-from-lbyl-to-eafp">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[What’s The Point of `itemgetter()` in Python? • [Club]]]></title><description><![CDATA[You&#8217;ve come across `operator.itemgetter()`, but you feel it&#8217;s redundant? Let&#8217;s explore]]></description><link>https://www.thepythoncodingstack.com/p/whats-the-point-of-itemgetter-in-python</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/whats-the-point-of-itemgetter-in-python</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Sat, 07 Feb 2026 22:35:09 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/45a996f6-13fe-4334-b7b3-717bbb6f4cda_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The first time I saw <code>itemgetter()</code>, I did what I&#8217;d normally do when I come across a new tool. I looked up the docs, opened a REPL, and explored a bit.</p><p>And my reaction at first was: &#8220;So what?! This tool doesn&#8217;t do anything I couldn&#8217;t already do with other features in Python.&#8221;</p><p>So, why does <code>itemgetter()</code> exist?</p><p>But, before answering this question, let&#8217;s answer a more basic one&#8230;</p><h2><strong>What Does </strong><code>itemgetter()</code><strong> Do?</strong></h2>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/whats-the-point-of-itemgetter-in-python">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[The Orchestra Conductor, The Senior Programmer, and AI • [Club]]]></title><description><![CDATA[A short opinion post with some thoughts on the changing programming landscape]]></description><link>https://www.thepythoncodingstack.com/p/the-orchestra-conductor-the-senior</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/the-orchestra-conductor-the-senior</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Thu, 22 Jan 2026 22:33:52 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/4a2917f6-4c9f-4648-9ff3-6aca095cbbc7_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I spent a few years learning to play the piano when I was a child. It was always clear I would never be a concert pianist. Or a pianist of any description. This is to say that I don&#8217;t know much about music. I still struggle to understand why an orchestra needs a conductor&#8211;don&#8217;t the musicians all have the score that they can play nearly perfectly?</p><p>And many people who comment about programming and AI know as much about programming as I know about music&#8230;and probably even less about AI.</p><p>But the orchestra conductor analogy seems a good one. Let me explore it further.</p>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/the-orchestra-conductor-the-senior">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[Parkruns, Python’s enumerate and zip, and Why Python Loops Are Different from Other Languages • [Club]]]></title><description><![CDATA[Don&#8217;t forget about enumerate() and zip() when coding in Python &#8226; A short post]]></description><link>https://www.thepythoncodingstack.com/p/parkruns-pythons-enumerate-and-zip</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/parkruns-pythons-enumerate-and-zip</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Fri, 09 Jan 2026 13:57:38 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/27fc31b3-78c7-4cf3-8e56-ce6f271540a9_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you live in the UK, you&#8217;re probably familiar with the Parkrun tradition: a friendly 5k run held every Saturday morning in hundreds of parks across the UK. Runners range from Olympians to people trying to lose some weight. It&#8217;s a well-oiled format replicated across all <a href="https://www.parkrun.org.uk/events/events/#geo=5.06/54.91/-3.42">893 Parkrun locations</a>.</p><p>And here&#8217;s how they deal with the finish line logistics. Runners don&#8217;t wear bibs with numbers. When they cross the finish line, they enter a &#8220;funnel&#8221; marked by plastic cones and are handed a token with their position number. They then proceed to another official, who scans their personal barcode, which runners carry in their pockets or on a wristband, and the position token they received a few seconds earlier. This process matches the runner with their finishing position.</p><p>What&#8217;s this got to do with Python loops? And how does it help us understand why Python does loops differently from other languages?</p><p>First step, let&#8217;s create the Parkrun funnel. I&#8217;ll just put the first five finishers in this example:</p><pre><code>&gt;&gt;&gt; funnel = [&#8221;Jonathan&#8221;, &#8220;Michael&#8221;, &#8220;Samantha&#8221;, &#8220;Jessica&#8221;, &#8220;Daniel&#8221;]</code></pre><p>Now, here&#8217;s something you definitely know already because it&#8217;s always one of the first things you&#8217;re taught when learning Python: <em><strong>Don&#8217;t</strong></em> loop through this list like this:</p><pre><code># Avoid this when coding in Python
&gt;&gt;&gt; i = 0
&gt;&gt;&gt; while i &lt; len(funnel):
...     name = funnel[i]
...     print(name)
...     i += 1
...    
Jonathan
Michael
Samantha
Jessica
Daniel</code></pre><p>This style mimics how other languages may work: you manually define and increment the index. To be fair, most people who shift from other languages are more likely to write the following version at some point:</p><pre><code># Also best to avoid this in Python
&gt;&gt;&gt; for i in range(len(funnel)):
...     name = funnel[i]
...     print(name)
...    
Jonathan
Michael
Samantha
Jessica
Daniel</code></pre><p>This version may seem more Pythonic since it uses Python tools such as <code>range()</code>, but still fails to make the most of Python&#8217;s iteration protocol. The Pythonic way of looping through this list is the following:</p><pre><code>&gt;&gt;&gt; for name in funnel:
...     print(name)
...    
Jonathan
Michael
Samantha
Jessica
Daniel</code></pre><p>A question that&#8217;s often asked but rarely answered is: Why is this version preferred over the other two? I&#8217;ll write another short post to answer this question soon as I want to keep these <em>The Club</em> posts short whenever possible. So, let me state just a few reasons (there are more) and then I&#8217;ll move on to my main topic for today.</p><ul><li><p>It&#8217;s more readable</p></li><li><p>It&#8217;s more efficient (try timing the versions above using <code>timeit</code>&#8212;remove the <code>print()</code> calls first)</p></li><li><p>It&#8217;s less prone to errors and bugs</p></li><li><p>It works with a broader selection of data structures, not just sequences</p></li></ul><p>While you wait for my follow-up post on this, you can read more about Python&#8217;s Iterator Protocol, iterables, and iterators here:</p><ul><li><p><a href="https://www.thepythoncodingstack.com/p/the-anatomy-of-a-for-loop">The Anatomy of a for Loop</a></p></li><li><p><a href="https://www.thepythoncodingstack.com/p/python-iterable-data-structures">Iterable: Python&#8217;s Stepping Stones</a></p></li><li><p><a href="https://www.thepythoncodingstack.com/p/iterators-in-python-data-structure-6">A One-Way Stream of Data &#8226; Iterators in Python</a></p></li></ul><p>But let&#8217;s move on.</p><p>Let&#8217;s say you want to print out the names alongside each runner&#8217;s position. You&#8217;d like the following output:</p><pre><code>1. Jonathan
2. Michael
3. Samantha
4. Jessica
5. Daniel</code></pre><p>&#8220;Aha!&#8221; I&#8217;m often told by some learners, &#8220;This is when you need to use the <code>for i in range(len(funnel))</code> idiom, since you need the index!&#8221;</p><p>Python&#8217;s <code>for</code> loop doesn&#8217;t explicitly use the index, so you don&#8217;t have access to the index within the <code>for</code> loop. Many revert to the non-Pythonic idioms for this.</p><p>But Python provides tools that let you stay within the pure Pythonic style. Python&#8217;s <code>for</code> loop needs an <a href="https://www.thepythoncodingstack.com/p/iterators-in-python-data-structure-6">iterator</a>&#8212;it will create one from the <a href="https://www.thepythoncodingstack.com/p/python-iterable-data-structures">iterable</a> you provide. All Python iteration needs iterators, not just <code>for</code> loops. Iterators are Python&#8217;s tool for any iteration.</p><p>And there are some bespoke iterators in Python that handle most of your iteration needs. I recently wrote a series about the <code>itertools</code> module. The <code>itertools</code> module contains many such tools. Here&#8217;s the series: <a href="https://www.thepythoncodingstack.com/p/the-itertools-series">The itertools Series</a>.</p><p>But there are also two built-in tools that many forget, but are extremely useful. The first one is <code>enumerate()</code>.</p><p>Here&#8217;s how you can use it to display the Parkrun results:</p><pre><code>&gt;&gt;&gt; for index, name in enumerate(funnel, start=1):
...     print(f&#8221;{index}. {name}&#8221;)
...    
1. Jonathan
2. Michael
3. Samantha
4. Jessica
5. Daniel</code></pre>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/parkruns-pythons-enumerate-and-zip">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[If You Love Queuing, Will You Also Love Priority Queuing? • [Club]]]></title><description><![CDATA[Exploring Python&#8217;s heapq]]></description><link>https://www.thepythoncodingstack.com/p/python-heapq-heap-priority-queue</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/python-heapq-heap-priority-queue</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Mon, 15 Dec 2025 16:53:19 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/515cd013-b57e-4e17-85ff-495630761833_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>You provide three tiers to your customers: Gold, Silver, and Bronze. And one of the perks of the higher tiers is priority over the others when your customers need you.</p><p>Gold customers get served first. When no Gold customers are waiting, you serve Silver customers. Bronze customers get served when there&#8217;s no one in the upper tiers waiting.</p><p>How do you set up this queue in your Python program?</p><p>You need to consider which data structure to use to keep track of the waiting customers and what code you&#8217;ll need to write to keep track of the complex queuing rules.</p><p>Sure, you could keep three separate lists (or better still, three <a href="https://thepythoncodingstack.substack.com/p/clearing-the-deque-python-linked-list">`deque` objects</a>). But that&#8217;s not fun! And what if you had more than three priority categories? Perhaps a continuous range of priorities rather than a discrete number?</p><p>There&#8217;s a Python tool for this!</p><p>So let&#8217;s start coding. First, create the data structure to hold the customer names in the queue:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GsIo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GsIo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png 424w, https://substackcdn.com/image/fetch/$s_!GsIo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png 848w, https://substackcdn.com/image/fetch/$s_!GsIo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png 1272w, https://substackcdn.com/image/fetch/$s_!GsIo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GsIo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png" width="1200" height="168" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:168,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:6542,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GsIo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png 424w, https://substackcdn.com/image/fetch/$s_!GsIo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png 848w, https://substackcdn.com/image/fetch/$s_!GsIo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png 1272w, https://substackcdn.com/image/fetch/$s_!GsIo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e709724-0fe6-425f-a08d-50aaa727f0e7_1200x168.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a><figcaption class="image-caption">All code blocks are available in text format at the end of this article &#8226; #1 &#8226; <em>The code images used in this article are created using <a href="https://snappify.cello.so/f4AsFrwgwov">Snappify</a>. [Affiliate link]</em></figcaption></figure></div><p>&#8220;You told me there&#8217;s a special tool for this? But this is just a bog-standard list, Stephen!!&#8221;</p><p>Don&#8217;t send your complaints just yet. Yes, that&#8217;s a list, but bear with me. We&#8217;ll use the list just as the structure to hold the data, but we&#8217;ll rely on another tool for the fun stuff. It&#8217;s time to import the <code>heapq</code> module, which is part of the Python standard library:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!32fP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!32fP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png 424w, https://substackcdn.com/image/fetch/$s_!32fP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png 848w, https://substackcdn.com/image/fetch/$s_!32fP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png 1272w, https://substackcdn.com/image/fetch/$s_!32fP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!32fP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png" width="1200" height="168" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:168,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7194,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!32fP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png 424w, https://substackcdn.com/image/fetch/$s_!32fP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png 848w, https://substackcdn.com/image/fetch/$s_!32fP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png 1272w, https://substackcdn.com/image/fetch/$s_!32fP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F426e85cf-714b-4b9d-a299-4c6b46ed6868_1200x168.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#2</figcaption></figure></div><p>This module contains the tools to create and manage a <em>heap queue</em>, which is also known as a <em>priority queue</em>. I&#8217;ll use the terms &#8216;heap queue&#8217; and &#8216;priority queue&#8217; interchangeably in this post. If you did a computer science degree, you&#8217;d have studied this at some point in your course. But if, like me and many others, you came to programming through a different route, then read on&#8230;</p><p>Let&#8217;s bundle the customer&#8217;s name and priority level into a single item. Jim is the first person to join the queue. He&#8217;s a Silver-tier member. Here&#8217;s what his entry would look like:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JJsO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JJsO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png 424w, https://substackcdn.com/image/fetch/$s_!JJsO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png 848w, https://substackcdn.com/image/fetch/$s_!JJsO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png 1272w, https://substackcdn.com/image/fetch/$s_!JJsO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JJsO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png" width="1200" height="168" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:168,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5345,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JJsO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png 424w, https://substackcdn.com/image/fetch/$s_!JJsO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png 848w, https://substackcdn.com/image/fetch/$s_!JJsO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png 1272w, https://substackcdn.com/image/fetch/$s_!JJsO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05810bb7-a9f6-48de-9117-f5a24c3df04f_1200x168.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#3</figcaption></figure></div><p>It&#8217;s a tuple with two elements. The integer <code>2</code> refers to the Silver tier, which has the second priority level. Gold members get a <code>1</code> and Bronze members&#8212;you guessed it&#8212;a <code>3</code>.</p><p>But don&#8217;t use <code>.append()</code> to add Jim to <code>service_queue</code>. Instead, let&#8217;s use <code>heapq.heappush()</code> to <em>push</em> an item onto the <em>heap</em>:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZhD3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZhD3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png 424w, https://substackcdn.com/image/fetch/$s_!ZhD3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png 848w, https://substackcdn.com/image/fetch/$s_!ZhD3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png 1272w, https://substackcdn.com/image/fetch/$s_!ZhD3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZhD3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png" width="1200" height="168" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b2117888-a126-46a8-8714-bbac3a195b46_1200x168.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:168,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:10374,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZhD3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png 424w, https://substackcdn.com/image/fetch/$s_!ZhD3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png 848w, https://substackcdn.com/image/fetch/$s_!ZhD3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png 1272w, https://substackcdn.com/image/fetch/$s_!ZhD3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2117888-a126-46a8-8714-bbac3a195b46_1200x168.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#4</figcaption></figure></div><p>Note that <code>heapq</code> is the name of a module. It&#8217;s not a data type&#8212;you don&#8217;t create an instance of type <code>heapq</code> as you would with data structures. You use a list as the data structure, which is why you pass the list <code>service_queue</code> as the first argument to <code>.heappush()</code>. The second argument is the item you want to push to the heap. In this case, it&#8217;s the tuple <code>(2, &#8220;Jim&#8221;)</code>. You&#8217;ll see later on why you need to put the integer <code>2</code> first in this tuple.</p><p>The <code>heapq</code> module doesn&#8217;t provide a new data structure. Instead, it provides algorithms for creating and managing a priority queue using a list.</p><p>Here&#8217;s the list <code>service_queue</code>:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!av-x!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!av-x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png 424w, https://substackcdn.com/image/fetch/$s_!av-x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png 848w, https://substackcdn.com/image/fetch/$s_!av-x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png 1272w, https://substackcdn.com/image/fetch/$s_!av-x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!av-x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png" width="1200" height="210" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:210,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9152,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!av-x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png 424w, https://substackcdn.com/image/fetch/$s_!av-x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png 848w, https://substackcdn.com/image/fetch/$s_!av-x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png 1272w, https://substackcdn.com/image/fetch/$s_!av-x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1872af1-f36e-431d-a5e9-759b9e7168f7_1200x210.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#5</figcaption></figure></div><p>&#8220;So what!&#8221; I hear you say. You would have got the same result if you had used <code>.append()</code>. Bear with me.</p><p>Pam comes in next. She&#8217;s a Gold-tier member:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rxm0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rxm0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png 424w, https://substackcdn.com/image/fetch/$s_!rxm0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png 848w, https://substackcdn.com/image/fetch/$s_!rxm0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png 1272w, https://substackcdn.com/image/fetch/$s_!rxm0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rxm0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png" width="1200" height="252" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:252,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:18676,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rxm0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png 424w, https://substackcdn.com/image/fetch/$s_!rxm0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png 848w, https://substackcdn.com/image/fetch/$s_!rxm0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png 1272w, https://substackcdn.com/image/fetch/$s_!rxm0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3260504-8b6c-4c12-804d-5a220e9cb9c4_1200x252.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#6</figcaption></figure></div><p>OK, cool, Pam was added at the beginning of the list since she&#8217;s a Gold member. What&#8217;s all the fuss?</p><p>Let&#8217;s see what happens after Dwight and Michael join the queue. Dwight is a Bronze-tier member. He&#8217;s followed in the queue by Michael, who&#8217;s a Silver-tier member:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!V1ab!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!V1ab!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png 424w, https://substackcdn.com/image/fetch/$s_!V1ab!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png 848w, https://substackcdn.com/image/fetch/$s_!V1ab!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png 1272w, https://substackcdn.com/image/fetch/$s_!V1ab!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!V1ab!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png" width="1200" height="252" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:252,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:23340,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!V1ab!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png 424w, https://substackcdn.com/image/fetch/$s_!V1ab!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png 848w, https://substackcdn.com/image/fetch/$s_!V1ab!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png 1272w, https://substackcdn.com/image/fetch/$s_!V1ab!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42adf231-57f2-4fb8-a94b-d4557515aa6e_1200x252.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#7</figcaption></figure></div><p>OK, this is what you&#8217;d expect once Dwight joins the queue, right? Dwight is a low-priority customer, so he&#8217;s last. Is this just a way of automatically ordering the list, then? Not so fast&#8230;</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ef1s!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ef1s!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png 424w, https://substackcdn.com/image/fetch/$s_!ef1s!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png 848w, https://substackcdn.com/image/fetch/$s_!ef1s!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png 1272w, https://substackcdn.com/image/fetch/$s_!ef1s!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ef1s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png" width="1200" height="252" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:252,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:25597,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ef1s!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png 424w, https://substackcdn.com/image/fetch/$s_!ef1s!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png 848w, https://substackcdn.com/image/fetch/$s_!ef1s!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png 1272w, https://substackcdn.com/image/fetch/$s_!ef1s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999335a1-290c-4d68-93b0-b730c63d38a7_1200x252.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#8</figcaption></figure></div><p>The fourth customer to walk in is Michael, who&#8217;s a Silver-tier customer. But he ends up in the last position in the list. What&#8217;s happening here?</p><p>It&#8217;s time to start understanding the heap queue algorithm.</p><h3><strong>Heap Queue &#8226; What&#8217;s Going On?</strong></h3><p>Let&#8217;s go back to when the queue was empty. The first person to join the queue was Jim (Silver tier). Let&#8217;s place Jim in a <em>node</em>:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1eJn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1eJn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png 424w, https://substackcdn.com/image/fetch/$s_!1eJn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png 848w, https://substackcdn.com/image/fetch/$s_!1eJn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png 1272w, https://substackcdn.com/image/fetch/$s_!1eJn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1eJn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png" width="1200" height="462" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:462,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:16005,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4673931c-30cb-4b1d-84ef-bef299e27353_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1eJn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png 424w, https://substackcdn.com/image/fetch/$s_!1eJn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png 848w, https://substackcdn.com/image/fetch/$s_!1eJn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png 1272w, https://substackcdn.com/image/fetch/$s_!1eJn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F563cab96-e2fa-4a40-85c5-9a0b7458f0b9_1200x462.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So far, there&#8217;s nothing too exciting. But let&#8217;s start defining some of the rules in the heap queue algorithm:</p><ul><li><p>Each node can have at most two child nodes&#8212;that&#8217;s two nodes connected to it.</p></li></ul><p>So let&#8217;s add more nodes as more customers join the queue.</p><p>Pam joined next. So Pam&#8217;s node starts as a child node linked to the only node you have so far:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-r1Z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-r1Z!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png 424w, https://substackcdn.com/image/fetch/$s_!-r1Z!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png 848w, https://substackcdn.com/image/fetch/$s_!-r1Z!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png 1272w, https://substackcdn.com/image/fetch/$s_!-r1Z!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-r1Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png" width="1200" height="679" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:679,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29378,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdabc9ac2-ec21-4054-85f1-8551927011fd_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-r1Z!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png 424w, https://substackcdn.com/image/fetch/$s_!-r1Z!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png 848w, https://substackcdn.com/image/fetch/$s_!-r1Z!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png 1272w, https://substackcdn.com/image/fetch/$s_!-r1Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ac3b6e5-f258-4dd1-8422-0d2b4337cc58_1200x679.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>However, here&#8217;s the second rule for dealing with a heap queue:</p><ul><li><p>A child cannot have a higher priority than its parent. If it does, swap places between child and parent.</p></li></ul><p>Recall that <code>1</code> represents the highest priority:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H4ty!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H4ty!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png 424w, https://substackcdn.com/image/fetch/$s_!H4ty!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png 848w, https://substackcdn.com/image/fetch/$s_!H4ty!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png 1272w, https://substackcdn.com/image/fetch/$s_!H4ty!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H4ty!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png" width="1200" height="669" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:669,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:34980,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1877e40a-2582-4ba0-acb2-ef419b915e41_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!H4ty!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png 424w, https://substackcdn.com/image/fetch/$s_!H4ty!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png 848w, https://substackcdn.com/image/fetch/$s_!H4ty!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png 1272w, https://substackcdn.com/image/fetch/$s_!H4ty!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3e1e596e-2b6a-4d94-928d-ccabc5ed5cb4_1200x669.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Pam (Gold tier / 1) is now the parent node, and Jim (Silver tier / 2) is now the child node and lies in the second layer in the hierarchy.</p><p>Bronze-tier member Dwight joined next. Recall that each parent node can have at most two child nodes. Since Pam&#8217;s node still has an empty slot, you add Dwight as a child node to Pam&#8217;s node:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NKXy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NKXy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png 424w, https://substackcdn.com/image/fetch/$s_!NKXy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png 848w, https://substackcdn.com/image/fetch/$s_!NKXy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png 1272w, https://substackcdn.com/image/fetch/$s_!NKXy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NKXy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png" width="1200" height="675" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/88f4569a-3943-4899-974d-e7114f153615_1200x675.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:675,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:36421,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e048e04-bd83-48f5-a243-e0ad097ed249_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NKXy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png 424w, https://substackcdn.com/image/fetch/$s_!NKXy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png 848w, https://substackcdn.com/image/fetch/$s_!NKXy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png 1272w, https://substackcdn.com/image/fetch/$s_!NKXy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88f4569a-3943-4899-974d-e7114f153615_1200x675.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s apply the second rule: the child node cannot have a higher priority than its parent. Dwight is a Bronze-tier member, and so he has a lower priority than Pam. All fine. No swaps needed.</p><p>Michael joined the queue next. He&#8217;s a Silver-tier member. Since Pam&#8217;s node already has two child nodes, you can&#8217;t add more child nodes to Pam. The second layer of the hierarchy is full. So, you take the first node in the second layer, and this now becomes a parent node. So you can add a child node to Jim:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Y5at!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Y5at!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!Y5at!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!Y5at!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!Y5at!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Y5at!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png" width="1200" height="900" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:900,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:24508,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Y5at!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!Y5at!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!Y5at!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!Y5at!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d9491f4-ff98-47c4-b0e2-9a0b27fe3de2_1200x900.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Time to apply the second rule. But Michael, who&#8217;s in the child node, has the same membership tier as Jim, who&#8217;s in the parent node. Python doesn&#8217;t stop here to resolve the tie. But you&#8217;ll explore this later in this post. For now, just take my word that no swap is needed.</p><p>Let&#8217;s look at the list <code>service_queue</code> again. Recall that this list is hosting the priority queue:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pcHl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pcHl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png 424w, https://substackcdn.com/image/fetch/$s_!pcHl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png 848w, https://substackcdn.com/image/fetch/$s_!pcHl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png 1272w, https://substackcdn.com/image/fetch/$s_!pcHl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pcHl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png" width="1200" height="210" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:210,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15448,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pcHl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png 424w, https://substackcdn.com/image/fetch/$s_!pcHl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png 848w, https://substackcdn.com/image/fetch/$s_!pcHl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png 1272w, https://substackcdn.com/image/fetch/$s_!pcHl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e6611-a2b0-42e9-9463-3ab9a250ad13_1200x210.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#9</figcaption></figure></div><p>The priority queue has one node in the top layer. So the first item in the list represents the only node in the top layer. That&#8217;s <code>(1, Pam)</code>.</p><p>The second and third items in the list represent the second layer. There can only be at most two items in this second layer. The fourth item in the list is therefore the start of the third layer. That&#8217;s why it&#8217;s fine for Michael to come after Dwight in the order in the list. It&#8217;s not the actual order in the list that matters, but the relationship between nodes in the heap tree.</p><p>But there&#8217;s more fun to come as we add more customers and start serving them&#8212;and therefore remove them from the priority queue! Let&#8217;s add some more customers first.</p><p>Angela, a Bronze-tier member, joins the queue next. Let&#8217;s add the new node to the tree first:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aQ4B!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aQ4B!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!aQ4B!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!aQ4B!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!aQ4B!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aQ4B!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png" width="1200" height="900" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:900,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:28987,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!aQ4B!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!aQ4B!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!aQ4B!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!aQ4B!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6d5b651f-d82e-40e2-b3dd-f3df8722329c_1200x900.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The relationship between parent and child doesn&#8217;t violate the heap queue rule. Angela (Bronze) has a lower priority than the person in the parent node, Jim (Silver):</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H-Bf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H-Bf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png 424w, https://substackcdn.com/image/fetch/$s_!H-Bf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png 848w, https://substackcdn.com/image/fetch/$s_!H-Bf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png 1272w, https://substackcdn.com/image/fetch/$s_!H-Bf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H-Bf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png" width="1344" height="252" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:252,&quot;width&quot;:1344,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:27220,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!H-Bf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png 424w, https://substackcdn.com/image/fetch/$s_!H-Bf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png 848w, https://substackcdn.com/image/fetch/$s_!H-Bf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png 1272w, https://substackcdn.com/image/fetch/$s_!H-Bf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe944e1df-73e4-40e7-912f-b8bc993d84ed_1344x252.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#10</figcaption></figure></div><p>One more client comes in. It&#8217;s Kevin, and he&#8217;s a Gold-tier member:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!l2mh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!l2mh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!l2mh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!l2mh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!l2mh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!l2mh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png" width="1200" height="900" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:900,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:31961,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!l2mh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!l2mh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!l2mh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!l2mh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F308fda37-1ed7-4169-bfef-6392466fb893_1200x900.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>There are no more free slots linked to Jim&#8217;s node, so you add Kevin as a child node linked to Dwight. But Kevin has a higher priority than Dwight, so you swap the nodes:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!I5iP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!I5iP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!I5iP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!I5iP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!I5iP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!I5iP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png" width="1200" height="900" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:900,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:36198,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!I5iP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!I5iP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!I5iP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!I5iP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56168380-485c-4fec-89c2-b76b29106fbb_1200x900.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>But now you need to compare Kevin&#8217;s node with its parent. Pam and Kevin both have the same membership level. They&#8217;re Gold-tier members.</p><p>But how does Python decide priority in this case?</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AaT7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AaT7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png 424w, https://substackcdn.com/image/fetch/$s_!AaT7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png 848w, https://substackcdn.com/image/fetch/$s_!AaT7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png 1272w, https://substackcdn.com/image/fetch/$s_!AaT7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AaT7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png" width="1200" height="210" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:210,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9583,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AaT7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png 424w, https://substackcdn.com/image/fetch/$s_!AaT7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png 848w, https://substackcdn.com/image/fetch/$s_!AaT7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png 1272w, https://substackcdn.com/image/fetch/$s_!AaT7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22b01007-780e-4e7c-9538-848b9a7862b4_1200x210.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#11</figcaption></figure></div><p>Python thinks that <code>(1, &#8220;Kevin&#8221;)</code> has a higher priority than <code>(1, &#8220;Pam&#8221;)</code>&#8212;in Python&#8217;s heap queue algorithm, an item takes priority if it&#8217;s <em>less than</em> another item. Python is comparing tuples. It doesn&#8217;t know anything about your multi-tier queuing system.</p><p>When Python compares tuples, it first compares the first element of each tuple and determines which is smaller. The whole tuple is considered smaller than the other if the first element is smaller than the matching first element in the other tuple. However, if there&#8217;s a tie, Python looks at the second element from each tuple.</p><p>Let&#8217;s briefly assume there&#8217;s a Gold-tier member called Adam:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vxei!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vxei!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png 424w, https://substackcdn.com/image/fetch/$s_!vxei!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png 848w, https://substackcdn.com/image/fetch/$s_!vxei!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png 1272w, https://substackcdn.com/image/fetch/$s_!vxei!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vxei!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png" width="1200" height="210" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:210,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:11018,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vxei!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png 424w, https://substackcdn.com/image/fetch/$s_!vxei!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png 848w, https://substackcdn.com/image/fetch/$s_!vxei!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png 1272w, https://substackcdn.com/image/fetch/$s_!vxei!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F526fdb6d-37d4-47cc-8c28-c915e3fec9d6_1200x210.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">#12</figcaption></figure></div><p>Python now considers <code>(1, &#8220;Adam&#8221;)</code> as the item with a higher priority.</p><p>The second element of each tuple is a string. Therefore, Python sorts these out using alphabetical order (<a href="https://en.wikipedia.org/wiki/Lexicographic_order">lexicographic</a> order, technically).</p><p>That&#8217;s why Kevin takes priority over Pam even though they&#8217;re both Gold-tier members. &#8216;K&#8217; comes before &#8216;P&#8217; in the alphabet! You must swap Kevin and Pam:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uD9W!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uD9W!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!uD9W!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!uD9W!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!uD9W!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uD9W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png" width="1200" height="900" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:900,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:36008,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uD9W!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png 424w, https://substackcdn.com/image/fetch/$s_!uD9W!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png 848w, https://substackcdn.com/image/fetch/$s_!uD9W!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png 1272w, https://substackcdn.com/image/fetch/$s_!uD9W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F421b0fc0-0a46-4afd-b30f-7bb83fb07a64_1200x900.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Note that the algorithm only needs to consider items along one branch of the tree hierarchy. Jim, Michael, and Angela weren&#8217;t disturbed to figure out where Kevin should go. This technique makes this algorithm efficient, especially as the number of items in the heap increases.</p><p>Incidentally, you can go back to when you added Michael to the queue and see why he didn&#8217;t leapfrog Jim even though they were both members of the same tier. &#8216;M&#8217; comes after &#8216;J&#8217; in the alphabet.</p><p>Now, we can argue that it&#8217;s not fair to give priority to someone just because their name comes first in alphabetical order. We&#8217;ll add timestamps later in this code to act as tie-breakers. But for now, let&#8217;s keep it simple and stick with this setup, where clients&#8217; names are used to break ties.</p><p>Let&#8217;s check that the <code>service_queue</code> list matches the diagram above:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!V1qX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!V1qX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png 424w, https://substackcdn.com/image/fetch/$s_!V1qX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png 848w, https://substackcdn.com/image/fetch/$s_!V1qX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png 1272w, https://substackcdn.com/image/fetch/$s_!V1qX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!V1qX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png" width="1200" height="294" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:294,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29820,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.thepythoncodingstack.com/i/181668518?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!V1qX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png 424w, https://substackcdn.com/image/fetch/$s_!V1qX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png 848w, https://substackcdn.com/image/fetch/$s_!V1qX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png 1272w, https://substackcdn.com/image/fetch/$s_!V1qX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ba34974-87a0-4157-b900-ecb0175b5af2_1200x294.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Kevin is in the first slot in the list, which represents the node at the top of the hierarchy. Jim and Pam are in the second layer, and Michael, Angela, and Dwight are the third generation of nodes. There&#8217;s still one more space in this layer. So, the next client would be added to this layer initially. But we&#8217;ll stop adding clients here in this post.</p><h3><strong>And How Does the Heap Queue Work When Removing Items?</strong></h3><p>It&#8217;s time to start serving these clients and removing them from the priority queue.</p>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/python-heapq-heap-priority-queue">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[I Don’t Like Magic • Exploring The Class Attributes That Aren’t Really Class Attributes • [Club]]]></title><description><![CDATA[This syntax, used for data classes and typing.NamedTuple, confused me when first learning about these topics. Here&#8217;s why, and why it&#8217;s no longer confusing.]]></description><link>https://www.thepythoncodingstack.com/p/i-dont-like-magic-exploring-the-class</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/i-dont-like-magic-exploring-the-class</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Tue, 18 Nov 2025 22:01:25 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/29c36185-05bf-45e2-aa70-510fca9f86aa_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I don&#8217;t like magic. I don&#8217;t mean the magic of the Harry Potter kind&#8212;that one I&#8217;d like if only I could have it. It&#8217;s the &#8220;magic&#8221; that happens behind the scenes when a programming language like Python does things out of sight. You&#8217;ll often find things you have to &#8220;just learn&#8221; along the Python learning journey. &#8220;That&#8217;s the way things are,&#8221; you&#8217;re told.</p><p>That&#8217;s the kind of magic I don&#8217;t like. I want to know how things work. So let me take you back to when I first learnt about named tuples&#8212;the <code>NamedTuple</code> in the <code>typing</code> module, not the other one&#8212;and data classes. They share a similar syntax, and it&#8217;s this shared syntax that confused me at first. I found these topics harder to understand because of this.</p><p>Their syntax is different from other stuff I had learnt up to that point. And I could not reconcile it with the stuff I knew. That bothered me. It also made me doubt the stuff I already knew. Here&#8217;s what I mean. Let&#8217;s look at a standard class first:</p><pre><code>class Person:
    classification = &#8220;Human&#8221;
&#8203;
    def __init__(self, name, age, profession):
        self.name = name
        self.age = age
        self.profession = profession</code></pre><p>You define a class attribute, <code>.classification</code>, inside the class block, but outside any of the special methods. All instances will share this class attribute. Then you define the <code>.__init__()</code> special method and create three instance attributes: <code>.name</code>, <code>.age</code>, and <code>.profession</code>. Each instance will have its own versions of these instance attributes. If you&#8217;re not familiar with class attributes and instance attributes, you can read my seven-part series on object-oriented programming: <a href="https://www.thepythoncodingstack.com/p/a-magical-tour-through-object-oriented">A Magical Tour Through Object-Oriented Programming in Python &#8226; Hogwarts School of Codecraft and Algorithmancy</a></p><p>Now, let&#8217;s assume you don&#8217;t actually need the class attribute and that this class will only store data. It won&#8217;t have any additional methods. You decide to use a data class instead:</p><pre><code>from dataclasses import dataclass
&#8203;
@dataclass
class Person:
    name: str
    age: int
    profession: str</code></pre><p>Or you prefer to use a named tuple, and you reach out for <code>typing.NamedTuple</code>:</p><pre><code>from typing import NamedTuple
&#8203;
class Person(NamedTuple):
    name: str
    age: int
    profession: str</code></pre><p>The syntax is similar. I&#8217;ll tell you why I used to find this confusing soon.</p><p>Whichever option you choose, you can create an instance using <code>Person(&#8221;Matthew&#8221;, 30, &#8220;Python Programmer&#8221;)</code>. And each instance you create will have its own instance attributes <code>.name</code>, <code>.age</code>, and <code>.profession</code>.</p><p>But wait a minute! The data class and the named tuple use syntax that&#8217;s similar to creating <em>class</em> attributes. You define these just inside the class block and not in an <code>.__init__()</code>  method. How come they create instance attributes? &#8220;That&#8217;s just how they work&#8221; is not good enough for me.</p><p>These aren&#8217;t class attributes. Not yet. There&#8217;s no value associated with these identifiers. Therefore, they can&#8217;t be class attributes, even though you write them where you&#8217;d normally add class attributes in a standard class. However, they can be class attributes if you include a default value:</p><pre><code>@dataclass
class Person:
    name: str
    age: int
    profession: str = &#8220;Python Programmer&#8221;</code></pre><p>The <code>.profession</code> attribute now has a string assigned to it. In a data class, this represents the default value. But if this weren&#8217;t a data class, you&#8217;d look at <code>.profession</code> and recognise it as a class attribute. But in a data class, it&#8217;s not a class attribute, it&#8217;s an instance attribute, as are <code>.name</code> and <code>.age</code>, which look like&#8230;what do they look like, really? They&#8217;re just type hints. Yes, type hints without any object assigned. Python type hints allow you to do this:</p><pre><code>&gt;&gt;&gt; first_name: str</code></pre><p>This line is valid in Python. It does not create the variable <code>name</code>. You can confirm this:</p><pre><code>&gt;&gt;&gt; first_name
Traceback (most recent call last):
  File &#8220;&lt;input&gt;&#8221;, line 1, in &lt;module&gt;
NameError: name &#8216;first_name&#8217; is not defined</code></pre><p>Although you cannot just write <code>first_name</code> if the identifier doesn&#8217;t exist, you can use <code>first_name: str</code>. This creates an annotation which serves as the type hint. Third-party tools now know that when you create the variable <code>first_name</code> and assign it a value, it ought to be a string.</p><p>So, let&#8217;s go back to the latest version of the <code>Person</code> data class with the default value for one of the attributes:</p><pre><code>@dataclass
class Person:
    name: str
    age: int
    profession: str = &#8220;Python Programmer&#8221;</code></pre><p>But let&#8217;s ignore the <code>@dataclass</code> decorator for now. Indeed, let&#8217;s remove this decorator:</p><pre><code>class Person:
    name: str
    age: int
    profession: str = &#8220;Python Programmer&#8221;</code></pre><p>You define a class with one class attribute, <code>.profession</code> and three type hints:</p><ul><li><p><code>name: str</code></p></li><li><p><code>age: int</code></p></li><li><p><code>profession: str</code></p></li></ul><p>How can we convert this information into instance attributes when creating an instance of the class? I won&#8217;t try to reverse engineer <code>NamedTuple</code> or data classes here. Instead, I&#8217;ll explore my own path to get a sense of what might be happening in those tools.</p><p>Let&#8217;s start hacking away&#8230;</p>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/i-dont-like-magic-exploring-the-class">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[The Misunderstood Hashable Types and Why Dictionaries Are Called Dictionaries • [Club]]]></title><description><![CDATA[What does hashable really mean? And what&#8217;s the real superpower in dictionaries?]]></description><link>https://www.thepythoncodingstack.com/p/the-misunderstood-hashable-types</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/the-misunderstood-hashable-types</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Sun, 09 Nov 2025 13:09:33 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/470a3dd0-3aed-438a-9320-0b6d1aab9fa8_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Pick up a dictionary. No, not that one. The <em>real</em> dictionary you have on your bookshelf, the one that has pages made of paper, which you use to look up the meaning of English words. Or whatever other language. But let&#8217;s assume it&#8217;s an English dictionary. Now, look up <em>zymology</em>.</p><p>I&#8217;ll wait&#8230;</p><p>Done? It probably didn&#8217;t take you too long to find <em>zymology</em>. Probably it took you longer to find the dictionary on your bookshelf, the one you haven&#8217;t used in years!</p><p>You know <em>z</em> is the last letter of the alphabet, so you opened the dictionary on a page towards the end of the book. Then you looked at a random word on the page or maybe the word listed in the header. Does it start with <em>z</em>? Yes? Look at the word&#8217;s second letter. No? Open the book to another page further in the dictionary.</p><p>You get the idea. I know you know how to find a word in a dictionary.</p><p>But now, imagine that you didn&#8217;t know the alphabet. Bear with me. Just assume you can recognise letters, so you can read words, but you don&#8217;t know the order of the letters in the alphabet. You don&#8217;t know that <em>A</em> comes first and <em>D</em> is the fourth one, and so on.</p><p>How would you find <em>zymology</em> now? And how long do you reckon it will take you to find the word?</p><p>There you go, now you understand the purpose of using hash values for hashable objects. Or you can read on&#8230;</p><p><em>Today, I also explore the topic in a short video where I can communicate differently. But if you prefer to just read, then go ahead and skip the video.</em></p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;bf9ba53f-bef7-4aca-af21-553bac4afa0f&quot;,&quot;duration&quot;:null}"></div><p></p><p>Let&#8217;s continue exploring dictionaries and hashable objects.</p>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/the-misunderstood-hashable-types">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[Impostors • How Even The Python Docs Get This Wrong* • [Club]]]></title><description><![CDATA[*Spoiler alert: The docs don&#8217;t really get it &#8220;wrong&#8221;]]></description><link>https://www.thepythoncodingstack.com/p/impostors-how-even-the-python-docs</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/impostors-how-even-the-python-docs</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Sun, 26 Oct 2025 08:52:48 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/9627d400-da61-4566-8d63-602fabc3b532_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Can you spot all the errors in the following paragraph? There are several:</p><blockquote><p>Two functions that enable you to work effectively with loops in Python are <code>zip()</code> and <code>enumerate()</code>. Along with the <code>range()</code> function, they&#8217;re some of the most common tools you&#8217;ll see in <code>for</code> loops. And when you master them, you can start exploring the functions in the <code>itertools</code> module.</p></blockquote><p>The correct number of errors in this text is either four or zero. Confused? I don&#8217;t blame you. And here&#8217;s a bit more confusion for you. It doesn&#8217;t matter either way.</p><p>Let&#8217;s talk about impostors in Python.</p><p>It&#8217;s likely that one of the first examples you saw when learning about <code>for</code> loops used the <code>range()</code> function as part of the <code>for</code> statement.</p><p>But you were fooled. That example, whichever one it was, didn&#8217;t do such a thing.</p>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/impostors-how-even-the-python-docs">
              Read more
          </a>
      </p>
   ]]></content:encoded></item><item><title><![CDATA[Are Tuples More Like Lists or Strings? And Why We Don't Really Care • [Club]]]></title><description><![CDATA[This post is not about tuples.]]></description><link>https://www.thepythoncodingstack.com/p/are-tuples-more-like-lists-or-strings</link><guid isPermaLink="false">https://www.thepythoncodingstack.com/p/are-tuples-more-like-lists-or-strings</guid><dc:creator><![CDATA[Stephen Gruppetta]]></dc:creator><pubDate>Mon, 20 Oct 2025 10:02:15 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/d019e417-1f45-4c0c-924d-9fde8950e81c_840x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This post is not about tuples. It&#8217;s not about lists or strings, either. It&#8217;s about Python, about its philosophy when dealing with data types, about how you should think about data types when coding in Python.</p><p>Tuples are usually introduced to Python learners after lists and strings. They&#8217;re almost always described as &#8220;like lists, but a bit different&#8221;.</p><p>And that&#8217;s not a bad thing. I say similar things when I introduce tuples to students. But then, fast-forward a few levels of proficiency, and I&#8217;m having different discussions with students. Are tuples like lists? Are they like strings? But the point is that those questions don&#8217;t really make sense. Other questions matter a lot more in Python. Let&#8217;s explore.</p><div><hr></div><p>This is the first post in <em>The Club</em>, so allow me a word or three before getting back to tuples. I sent out an email last week about the new chapter in <em>The Python Coding Stack</em>. Paid subscribers are now members of <em>The Club</em>. I&#8217;ll write more frequent, shorter posts for <em>The Club</em>, sometimes accompanied by short videos (videos will complement the text, they won&#8217;t replace it). And all the other stuff: forum, Q&amp;As, code reviews, and more.</p><p>I recall when I first started <em>The Python Coding Stack</em>, I agonised over what the first article should be about. It&#8217;s the first. It should be special. But then I just published about whatever topic I was thinking about at the time.</p><p>I took the same route when writing the first post on <em>The Club</em> &#8211; this one. And I&#8217;ll do this every week. I&#8217;ll write about whatever comes up in my coding, in my teaching, whatever I think you&#8217;ll find interesting. Or send me your questions, and they may inspire me to write about them. Don&#8217;t be shy.</p><p>Now back to tuples.</p><div><hr></div><p>A long time ago, on a social media platform (now) far, far away, I wrote a series of threads exploring real-world objects from the perspective of Python data types. I recall one of them was about a row of houses. Would a dictionary, or a list, or a tuple, or something else be the &#8220;ideal data structure&#8221; if I wanted to represent this row of houses as a data type? But there was another question I needed to ask first.</p>
      <p>
          <a href="https://www.thepythoncodingstack.com/p/are-tuples-more-like-lists-or-strings">
              Read more
          </a>
      </p>
   ]]></content:encoded></item></channel></rss>