<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anithaallam on Medium]]></title>
        <description><![CDATA[Stories by Anithaallam on Medium]]></description>
        <link>https://medium.com/@anithaallam8?source=rss-b93d06541d1f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*xz1o5yFxPP_b9z3t</url>
            <title>Stories by Anithaallam on Medium</title>
            <link>https://medium.com/@anithaallam8?source=rss-b93d06541d1f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 05:58:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anithaallam8/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Python Uses Data Structures Behind the Scenes: Lists, Tuples, Sets, and Dictionaries]]></title>
            <link>https://medium.com/@anithaallam8/how-python-uses-data-structures-behind-the-scenes-lists-tuples-sets-and-dictionaries-c52ba00ece9c?source=rss-b93d06541d1f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c52ba00ece9c</guid>
            <category><![CDATA[python-data-structures]]></category>
            <dc:creator><![CDATA[Anithaallam]]></dc:creator>
            <pubDate>Sat, 06 Jun 2026 06:48:31 GMT</pubDate>
            <atom:updated>2026-06-06T06:48:31.454Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Python provides several built-in data structures, including Lists, Tuples, Sets, and Dictionaries, that make storing and managing data simple and efficient. Most developers use these structures regularly, but few consider how Python handles them internally.</p><p>Behind the scenes, Python uses techniques such as dynamic arrays and hash tables to optimize storage, retrieval, and modification of data. Understanding these internal implementations can help developers write more efficient code and choose the right data structure for different situations.</p><p>In this article, we’ll explore how Python manages Lists, Tuples, Sets, and Dictionaries behind the scenes, along with real-world examples that explain why these data structures perform the way they do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*WrBDzHg1OjUAfRJE" /><figcaption>Python Data Structures</figcaption></figure><h3>1.List in Python :-</h3><p>A <strong>list</strong> is an ordered, mutable (changeable) collection of items. It can hold elements of any type — integers, strings, even other lists.</p><p>Example:- creating a Fruits list</p><pre>fruits=[&quot;Apple&quot;,&quot;Banana&quot;,&quot;orange&quot;,&quot;cherry&quot;,&quot;grapes&quot;]<br>print(fruits)<br>#output [&#39;Apple&#39;, &#39;Banana&#39;, &#39;orange&#39;, &#39;cherry&#39;, &#39;grapes&#39;]</pre><p><strong>What Python Does Internally</strong></p><p>Internally, Python’s list is a <strong>dynamic array</strong>. When you create a list, Python allocates a contiguous block of memory for object references (pointers). If you keep appending items and run out of space, Python automatically allocates a larger block and copies everything over. This is called <strong>over-allocation</strong>, and it makes repeated appends efficient on average — a concept known as <em>amortized O(1)</em> time complexity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/0*On0FY_irFVE_6KCG.jpg" /></figure><p><strong>List Slicing:-</strong></p><p>Slicing lets you extract a portion of a list using list[start:end]. The end index is <em>not</em> included.</p><pre>n=[10,2,3,45,67,85,345,34,4356,78,56,1234,55,67]<br>n[2:11:2]<br>#output [3, 67, 345, 4356, 56]</pre><p><strong>List methods:-</strong></p><ol><li>Append ()- adding a element into a list</li></ol><pre>fruits=[&quot;Apple&quot;,&quot;Banana&quot;,&quot;orange&quot;,&quot;cherry&quot;,&quot;grapes&quot;]<br>fruits.append(&quot;mangoes&quot;)<br>print(fruits)<br>#output [&#39;Apple&#39;, &#39;Banana&#39;, &#39;orange&#39;, &#39;cherry&#39;, &#39;grapes&#39;, &#39;mangoes&#39;]</pre><p>2. Sort ()- It can be sorted in ascending or descending order using the sort() method or the sorted() function.</p><pre>numbers = [50, 20, 40, 10, 30]<br>numbers.sort()<br>print(numbers)<br>#output [10, 20, 30, 40, 50]</pre><p>3. Reverse ()- Reverses the order of elements in a list</p><pre>numbers = [50, 20, 40, 10, 30]<br>numbers.reverse()<br>print(numbers)<br>#output [30, 10, 40, 20, 50]</pre><p>4. Pop ()-remove and return an element from the list</p><pre>numbers = [50, 20, 40, 10, 30]<br>numbers.pop()<br>print(numbers)<br>#output [50, 20, 40, 10]</pre><p>5. Insert ()-adds an element at a specified position</p><pre>numbers = [50, 20, 40, 10, 30]<br>numbers.insert(3,100)<br>print(numbers)<br>#output [50, 20, 40, 100, 10, 30]</pre><p>6. Remove ()- removes the first occurrence of a specified value</p><pre>numbers = [50, 20, 40, 10, 30]<br>numbers.remove(10)<br>print(numbers)<br># output [50, 40, 20, 30]</pre><h3>2.Tuple in python:-</h3><p>A <strong>tuple</strong> is an ordered, <strong>immutable</strong> sequence of values. Once created, its contents cannot be changed. Use round brackets ( ) to define one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*w9yECSVrGs5n_2uA.jpg" /></figure><pre>red = (255, 0, 0)<br>r, g, b = red<br>print(f&quot;Red channel: {r}&quot;)<br># Tuples as dictionary keys (lists cannot be used as keys!)<br>coordinates = {(0, 0): &quot;origin&quot;, (1, 2): &quot;point A&quot;}<br>print(coordinates[(1, 2)])<br># Trying to modify a tuple raises an error<br>try:<br>    red[0] = 100<br>except TypeError as e:<br>    print(f&quot;Error: {e}&quot;)</pre><pre>Red channel: 255<br>point A<br>Error: ‘tuple’ object does not support item assignment</pre><h3>Behind the Scenes</h3><p>Because tuples are immutable, Python can store them more compactly in memory and even intern (cache and reuse) small tuples. Accessing elements is slightly faster than a list because Python doesn’t need to worry about mutation. Additionally, tuples are hashable, meaning they can serve as dictionary keys — something lists can never do.</p><p><strong>Tuple methods:</strong></p><ol><li>index ()- it indicates that position of element</li></ol><pre>numbers = (50, 20, 40, 10, 30,45,67,89,34,23,78)<br>print(numbers.index(30))<br># output 4</pre><p>2.count ()- it indicates how many times a element can appear</p><pre>numbers = (50, 20, 40, 30, 30,45,67,89,30,23,78)<br>print(numbers.count(30))<br>#output 3</pre><p><strong>Tuple Unpacking()</strong></p><p>One of the most useful features of tuples — you can assign multiple variables in a single line</p><pre>data = 10, 20, 30,40,50,60<br>print(data)<br>print(type(data))<br>#output (10, 20, 30,40,50,60)<br>&lt;class &#39;tuple&#39;&gt;<br><br>data = (10, 20, 30)<br>a, b, c = data<br>print(a)<br>print(b)<br>print(c)<br>#output 10 20 30</pre><h3>3.Sets in Python</h3><blockquote><em>Sets are designed as unordered collections. Although they may appear in a consistent order during a program run, you should never rely on that order.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*fRK-wXaK5CBSKR4_.jpg" /></figure><pre># Sets automatically remove duplicates<br>visitors = {&quot;Alice&quot;, &quot;Bob&quot;, &quot;Alice&quot;, &quot;Charlie&quot;, &quot;Bob&quot;}<br>print(&quot;Unique visitors:&quot;, visitors)<br># Set operations - like mathematical sets<br>python_devs = {&quot;Alice&quot;, &quot;Bob&quot;, &quot;Diana&quot;}<br>js_devs     = {&quot;Bob&quot;, &quot;Charlie&quot;, &quot;Diana&quot;}<br>print(&quot;Both languages:&quot;,  python_devs &amp; js_devs)   # Intersection<br>print(&quot;Either language:&quot;, python_devs | js_devs)   # Union<br>print(&quot;Python only:&quot;,     python_devs - js_devs)   # Difference<br># Lightning-fast membership test - O(1) on average<br>print(&quot;Is Alice a Python dev?&quot;, &quot;Alice&quot; in python_devs)</pre><p>Unique visitors: {‘Alice’, ‘Bob’, ‘Charlie’}<br>Both languages: {‘Bob’, ‘Diana’}<br>Either language: {‘Alice’, ‘Bob’, ‘Charlie’, ‘Diana’}<br>Python only: {‘Alice’}<br>Is Alice a Python dev? True</p><h4>The Hash Table Magic</h4><p>Sets are implemented as hash tables — the same data structure used by Python dictionaries. When you add an element, Python computes its <em>hash value</em> (a unique integer fingerprint) and stores it at a calculated position in the table. This is why:</p><ul><li>Lookups are O(1) on average — no scanning required</li><li>Only hashable (immutable) objects can be stored in sets</li><li>Order is not preserved — the hash table positions don’t follow insertion order</li></ul><p><strong>Set methods</strong></p><ol><li>add ()- adds a single element to the set</li></ol><pre>s={&quot;hello&quot;,10,20,30,40,50,45,&quot;anitha&quot;}<br>s.add(300)<br>print(s)<br># output {50, &#39;anitha&#39;, 20, &#39;hello&#39;, 40, 300, 45, 30}</pre><p>2· remove ()- removes the specified elements from the set</p><pre>s={&quot;hello&quot;,10,20,30,40,50,45,&quot;anitha&quot;}<br>s.remove(&quot;hello&quot;)<br>print(s)<br>#output {40, 10, 45, 50, &#39;anitha&#39;, 20, 30}</pre><p>3· pop ()- removes and returns a random element from the set</p><pre>s={&quot;hello&quot;,10,20,30,40,50,45,&quot;anitha&quot;}<br>s.pop()<br>#output &#39;hello&#39;</pre><p>4· clear ()-remove all elements from the set</p><pre>s={&quot;hello&quot;,10,20,30,40,50,45,&quot;anitha&quot;}<br>s.clear()<br>print(s)<br># output set()</pre><h4>set operations:-</h4><p>1. set. Union (<strong>)-</strong>combines all unique elements from both sets</p><pre>v1={10,20,30,56,57,89,60,45,23,86}<br>v2={10,57,89,23,45,79,78,19,56,34,35}<br>v1.union(v2)<br>#output {10, 20, 23, 30, 34, 35, 45, 56, 57, 60, 78, 79, 86, 89}</pre><p>· set. Intersection () — return common elements present in both sets</p><pre>v1={10,20,30,56,57,89,60,45,23,86}<br>v2={10,57,89,23,45,79,78,19,56,34,35}<br>v1.intersection(v2)<br>#output {10, 23, 45, 56, 57, 89}</pre><h3>4. Dictionaries in Python</h3><p>A dictionary stores data as key-value pairs — like a real dictionary where each word (key) maps to a definition (value). Use curly braces { } with a colon separating each key and value.</p><p>A dictionary is an ordered and mutable collection of key-value pairs. Duplicate keys are not allowed, but duplicate values are permitted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/0*ForTAQS-Q9sqlMSz.png" /></figure><pre># Student profile as a dictionary<br>student = {<br>    &quot;name&quot;:   &quot;Priya Sharma&quot;,<br>    &quot;age&quot;:    21,<br>    &quot;grades&quot;: {&quot;Math&quot;: 92, &quot;Python&quot;: 98, &quot;DSA&quot;: 88}<br>}<br># Accessing and updating<br>print(student[&quot;name&quot;])<br>student[&quot;age&quot;] = 22<br># Word frequency counter using dict<br>sentence = &quot;python is great and python is fun&quot;<br>freq = {}<br>for word in sentence.split():<br>    freq[word] = freq.get(word, 0) + 1<br>print(freq)<br># Iterating<br>for subject, score in student[&quot;grades&quot;].items():<br>    print(f&quot;  {subject}: {score}/100&quot;)</pre><p>Priya Sharma<br>{‘python’: 2, ‘is’: 2, ‘great’: 1, ‘and’: 1, ‘fun’: 1}<br> Math: 92/100<br> Python: 98/100<br> DSA: 88/100</p><h4>How Python 3.7+ Keeps Order</h4><p>Prior to Python 3.7, dictionaries did <em>not</em> guarantee insertion order. From Python 3.7 onward, dicts maintain insertion order as part of the language spec. Internally, this is achieved with a compact hash table that stores an array of indices separately from the actual key-value entries — making modern Python dicts both fast and memory-efficient.</p><p><strong>Dictionary methods:</strong></p><p>1· keys ()- Returns all keys in the dictionary</p><pre>student = {<br>    &quot;name&quot;:   &quot;Priya&quot;,<br>    &quot;marks&quot;:  [97, 34, 45],<br>    &quot;class&quot;:  10<br>}<br>print(student.keys())<br>#ouput dict_keys([&#39;name&#39;, &#39;marks&#39;, &#39;class&#39;])</pre><p>2.values()-Returns all values in the dictionary</p><pre>student = {<br>    &quot;name&quot;:   &quot;Priya&quot;,<br>    &quot;marks&quot;:  [97, 34, 45],<br>    &quot;class&quot;:  10<br>}<br>print(student.values())<br>#output dict_values([&#39;Priya&#39;, [97, 34, 45], 10])</pre><p>3· items ()- Returns all key-value pairs as tuple</p><pre>student = {<br>    &quot;name&quot;:   &quot;Priya&quot;,<br>    &quot;marks&quot;:  [97, 34, 45],<br>    &quot;class&quot;:  10<br>}<br>print(student.items())<br>#output dict_items([(&#39;name&#39;, &#39;Priya&#39;), (&#39;marks&#39;, [97, 34, 45]), (&#39;class&#39;, 10)])</pre><p>4· update ()-Adds a new key-value pair or update an existing one</p><pre>student = {<br>    &quot;name&quot;:   &quot;Priya&quot;,<br>    &quot;marks&quot;:  [97, 34, 45],<br>    &quot;class&quot;:  10<br>}<br>student.update({&quot;name&quot;:&quot;Anitha&quot;})<br>print(student)<br>#output {&#39;name&#39;: &#39;Anitha&#39;, &#39;marks&#39;: [97, 34, 45], &#39;class&#39;: 10}</pre><h3>Conclusion</h3><p>Python’s four core data structures aren’t just containers — they’re carefully engineered tools with distinct performance characteristics and use cases. Here’s the mental model to take away:</p><ul><li>Use a list when you need an ordered, changeable sequence.</li><li>Use a tuple when your data must not change — it’s faster and signals intent.</li><li>Use a set when you need uniqueness or blazing-fast membership tests.</li><li>Use a dictionary when you need to map keys to values — it’s the backbone of most Python programs.</li></ul><p>Understanding what happens <em>behind the scenes</em> — dynamic arrays, hash tables, memory over-allocation — gives you the insight to choose the right structure and write code that is not just correct, but genuinely efficient.</p><p>Happy coding! 🐍</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c52ba00ece9c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>