<?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 Pratikshagholap on Medium]]></title>
        <description><![CDATA[Stories by Pratikshagholap on Medium]]></description>
        <link>https://medium.com/@pratikshagholap90?source=rss-609ff49f1caf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DodhOtsVuCK5RXk6</url>
            <title>Stories by Pratikshagholap on Medium</title>
            <link>https://medium.com/@pratikshagholap90?source=rss-609ff49f1caf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:57:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pratikshagholap90/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[Python Dictionaries: The One Data Structure You’ll Use Every Single Day]]></title>
            <link>https://medium.com/@pratikshagholap90/python-dictionaries-the-one-data-structure-youll-use-every-single-day-f9c51d184247?source=rss-609ff49f1caf------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9c51d184247</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Pratikshagholap]]></dc:creator>
            <pubDate>Mon, 05 Oct 2026 04:11:46 GMT</pubDate>
            <atom:updated>2026-10-05T04:11:46.349Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kmlGT2oz2LRlp20JnWCUmA.png" /></figure><h3>From counting words to tracking expenses, here’s how one simple data structure quietly powers much of the Python code you’ll write.</h3><p>Imagine you’re building a small app. You need to store a student’s name, age, course, and grade.</p><p>Your first instinct might be a list:</p><pre>student = [&quot;Aarav&quot;, 20, &quot;Computer Science&quot;, &quot;A&quot;]</pre><p>Now, quick question:</p><p><strong>What is </strong><strong>student[2]?</strong></p><p>You have to stop and think.</p><p>That’s the problem with lists when you’re working with labeled data: you have to remember what each position means.</p><p>Dictionaries solve this problem by letting you access information using a <strong>name instead of a position</strong>.</p><p>That simple idea is one reason dictionaries appear everywhere in real-world Python programming.</p><p>Let’s make them click.</p><h3>What Is a Dictionary?</h3><p>A <strong>dictionary</strong> stores data as <strong>key-value pairs</strong>.</p><p>Think about a real dictionary. You look up a word and get its meaning.</p><p>Python dictionaries work in a similar way:</p><pre>student = {<br>    &quot;name&quot;: &quot;Aarav&quot;,<br>    &quot;age&quot;: 20,<br>    &quot;course&quot;: &quot;Computer Science&quot;<br>}</pre><pre>print(student[&quot;name&quot;])</pre><p><strong>Output:</strong></p><pre>Aarav</pre><p>Instead of remembering that the student’s name is at position 0, you simply ask for &quot;name&quot;.</p><p>That’s much easier to read and understand.</p><h3>Why Do Dictionaries Matter?</h3><p>Real-world data is rarely just a collection of values. Most of the time, the values have labels.</p><p>For example:</p><p><strong>User profile</strong></p><pre>user = {<br>    &quot;username&quot;: &quot;aarav20&quot;,<br>    &quot;email&quot;: &quot;aarav@example.com&quot;,<br>    &quot;city&quot;: &quot;Pune&quot;<br>}</pre><p><strong>Product information</strong></p><pre>product = {<br>    &quot;name&quot;: &quot;Laptop&quot;,<br>    &quot;price&quot;: 55000,<br>    &quot;stock&quot;: 10<br>}</pre><p><strong>Weather information</strong></p><pre>weather = {<br>    &quot;temperature&quot;: 28,<br>    &quot;humidity&quot;: 65,<br>    &quot;wind_speed&quot;: 12<br>}</pre><p>Dictionaries are also closely related to <strong>JSON</strong>, which is widely used when applications and APIs exchange data.</p><p>Once you’re comfortable with dictionaries, working with API responses and structured data becomes much easier.</p><h3>How Does a Dictionary Work?</h3><p>At a high level, Python uses <strong>hashing</strong> to efficiently find values associated with keys.</p><p>You don’t need to understand the internal implementation to start using dictionaries, but there are a few important rules to remember:</p><ul><li><strong>Keys must be unique.</strong></li><li><strong>Keys must be hashable</strong>, which usually means using immutable types such as strings, numbers, or tuples.</li><li><strong>Values can be almost anything</strong> — numbers, strings, lists, or even other dictionaries.</li><li>Python dictionaries <strong>preserve insertion order</strong>.</li></ul><p>Here’s a quick reference:</p><p>OperationExampleCreated = {&quot;a&quot;: 1}Readd[&quot;a&quot;]Safe readd.get(&quot;b&quot;, 0)Add/Updated[&quot;b&quot;] = 2Deletedel d[&quot;a&quot;]Check key&quot;a&quot; in d</p><h3>A Practical Example: Counting Words</h3><p>One of the most useful dictionary patterns is <strong>counting things</strong>.</p><p>Suppose we want to count how many times each word appears in a sentence.</p><pre>text = &quot;to be or not to be&quot;<br>counts = {}</pre><pre>for word in text.split():<br>    counts[word] = counts.get(word, 0) + 1</pre><pre>print(counts)</pre><p><strong>Output:</strong></p><pre>{&#39;to&#39;: 2, &#39;be&#39;: 2, &#39;or&#39;: 1, &#39;not&#39;: 1}</pre><p>Pretty useful, right?</p><h3>Let’s understand the code</h3><p>First:</p><pre>text.split()</pre><p>breaks the sentence into individual words.</p><p>Then we loop through every word:</p><pre>for word in text.split():</pre><p>Next:</p><pre>counts.get(word, 0)</pre><p>asks:</p><blockquote><em>“Does this word already have a count? If not, give me 0.”</em></blockquote><p>Then we add 1:</p><pre>counts[word] = counts.get(word, 0) + 1</pre><p>This small pattern is incredibly useful for <strong>counting and grouping data</strong>.</p><h3>Looping Through a Dictionary</h3><p>You can easily loop through both keys and values using .items():</p><pre>student = {<br>    &quot;name&quot;: &quot;Aarav&quot;,<br>    &quot;age&quot;: 20,<br>    &quot;course&quot;: &quot;Computer Science&quot;<br>}</pre><pre>for key, value in student.items():<br>    print(f&quot;{key}: {value}&quot;)</pre><p><strong>Output:</strong></p><pre>name: Aarav<br>age: 20<br>course: Computer Science</pre><p>This is especially useful when you don’t know all the keys in advance.</p><h3>Dictionary Comprehensions</h3><p>Python also gives you a compact way to create dictionaries called a <strong>dictionary comprehension</strong>.</p><p>For example, let’s create a dictionary containing the squares of numbers from 1 to 5:</p><pre>squares = {n: n ** 2 for n in range(1, 6)}</pre><pre>print(squares)</pre><p><strong>Output:</strong></p><pre>{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}</pre><p>Once you’re comfortable with normal dictionaries, dictionary comprehensions are a great next step.</p><h3>Real-World Example: A Mini Expense Tracker</h3><p>Let’s look at something more practical.</p><p>Imagine you’re a college student tracking your daily expenses.</p><p>We can store each expense as a dictionary:</p><pre>expenses = [<br>    {&quot;category&quot;: &quot;food&quot;, &quot;amount&quot;: 250},<br>    {&quot;category&quot;: &quot;travel&quot;, &quot;amount&quot;: 120},<br>    {&quot;category&quot;: &quot;food&quot;, &quot;amount&quot;: 180},<br>    {&quot;category&quot;: &quot;books&quot;, &quot;amount&quot;: 600},<br>    {&quot;category&quot;: &quot;travel&quot;, &quot;amount&quot;: 90},<br>]</pre><p>Now we want to calculate the total amount spent in each category.</p><pre>totals = {}</pre><pre>for expense in expenses:<br>    category = expense[&quot;category&quot;]<br>    totals[category] = totals.get(category, 0) + expense[&quot;amount&quot;]</pre><pre>for category, total in sorted(<br>    totals.items(),<br>    key=lambda item: item[1],<br>    reverse=True<br>):<br>    print(f&quot;{category:&lt;8} ₹{total}&quot;)</pre><p><strong>Output:</strong></p><pre>books    ₹600<br>food     ₹430<br>travel   ₹210</pre><p>In just a few lines, we’ve grouped expenses by category and sorted them by total spending.</p><p>The same pattern can be used for:</p><ul><li>Sales reports</li><li>Survey results</li><li>Student records</li><li>Website analytics</li><li>Log analysis</li><li>Inventory systems</li></ul><p>And if you replace the hardcoded data with information from a CSV file or an API, you can start building a real application.</p><h3>Common Mistakes Beginners Make</h3><h3>1. Accessing a Missing Key</h3><p>This can cause a KeyError:</p><pre>student[&quot;grade&quot;]</pre><p>If &quot;grade&quot; doesn&#39;t exist, Python raises an error.</p><p>A safer approach is:</p><pre>student.get(&quot;grade&quot;)</pre><p>Or provide a default value:</p><pre>student.get(&quot;grade&quot;, &quot;Not Available&quot;)</pre><h3>2. Changing a Dictionary While Looping Through It</h3><p>Avoid adding or removing keys while directly iterating over the dictionary.</p><p>For example:</p><pre>for key in d:<br>    del d[key]</pre><p>This can raise a RuntimeError.</p><p>If you need to modify the dictionary, iterate over a separate list of its keys:</p><pre>for key in list(d):<br>    del d[key]</pre><h3>3. Using a List as a Key</h3><p>This won’t work:</p><pre>d = {[1, 2]: &quot;oops&quot;}</pre><p>You’ll get:</p><pre>TypeError: unhashable type</pre><p>If you need a collection as a key, use a tuple instead:</p><pre>d = {(1, 2): &quot;correct&quot;}</pre><h3>4. Thinking Assignment Creates a Copy</h3><p>Consider this:</p><pre>a = {&quot;x&quot;: 1}<br>b = a</pre><pre>b[&quot;x&quot;] = 99</pre><pre>print(a)</pre><p><strong>Output:</strong></p><pre>{&#39;x&#39;: 99}</pre><p>Why?</p><p>Because a and b refer to the <strong>same dictionary</strong>.</p><p>If you want a separate shallow copy:</p><pre>b = a.copy()</pre><h3>5. Using Inconsistent Key Names</h3><p>Be consistent with your keys.</p><p>For example, avoid mixing:</p><pre>&quot;Name&quot;<br>&quot;name&quot;<br>&quot;student_name&quot;</pre><p>Choose a naming style and stick with it.</p><p>Consistent naming makes your code easier to understand and maintain.</p><h3>Tips and Best Practices</h3><p>Here are a few habits that will make your dictionary code cleaner:</p><h3>Use .get() when a key might be missing</h3><pre>age = student.get(&quot;age&quot;, 0)</pre><h3>Use Counter for counting</h3><p>Python provides a specialized tool for counting:</p><pre>from collections import Counter</pre><pre>counts = Counter(text.split())</pre><pre>print(counts)</pre><h3>Use defaultdict when appropriate</h3><p>If you’re repeatedly creating missing values, defaultdict can make your code cleaner.</p><h3>Merge dictionaries when needed</h3><p>Python 3.9+ provides the | operator:</p><pre>merged = d1 | d2</pre><h3>Use meaningful keys</h3><p>Prefer:</p><pre>&quot;user_email&quot;</pre><p>over:</p><pre>&quot;e&quot;</pre><p>Clear names make your code much easier to understand.</p><h3>Don’t overuse nested dictionaries</h3><p>Dictionaries inside dictionaries can be useful, but deeply nested structures can quickly become difficult to read.</p><h3>Try It Yourself</h3><p>Now it’s your turn.</p><p>Open your Python editor and try these three challenges.</p><h3>Challenge 1: Character Counter</h3><p>Write a program that counts how many times each character appears in:</p><pre>mississippi</pre><h3>Challenge 2: Phone Book</h3><p>Create a dictionary containing five friends and their phone numbers.</p><p>Then ask the user for a name and display the corresponding phone number.</p><p>If the name doesn’t exist, display:</p><pre>Not found</pre><h3>Challenge 3: Flip the Dictionary</h3><p>Given:</p><pre>data = {<br>    &quot;a&quot;: 1,<br>    &quot;b&quot;: 2,<br>    &quot;c&quot;: 3<br>}</pre><p>Create a new dictionary where the keys and values are swapped.</p><p>Expected result:</p><pre>{<br>    1: &quot;a&quot;,<br>    2: &quot;b&quot;,<br>    3: &quot;c&quot;<br>}</pre><p><strong>Hint:</strong> A dictionary comprehension can solve this in one line.</p><h3>Why Should You Learn Dictionaries?</h3><h3>1. They’re everywhere</h3><p>Dictionaries are commonly used when working with JSON, configuration data, API responses, and structured records.</p><h3>2. They make code readable</h3><p>Compare:</p><pre>student[2]</pre><p>with:</p><pre>student[&quot;course&quot;]</pre><p>The second one immediately tells you what the data represents.</p><h3>3. They’re useful in coding interviews</h3><p>Many programming problems involving <strong>counting, grouping, frequency analysis, and fast lookups</strong> can be solved using dictionaries.</p><h3>4. The concept transfers to other languages</h3><p>Other programming languages have similar concepts, such as <strong>hash maps, associative arrays, and objects</strong>.</p><p>So you’re not just learning a Python feature. You’re learning an important programming concept.</p><h3>Key Takeaways</h3><p>If you remember only a few things from this article, remember these:</p><ul><li>A dictionary stores data as <strong>key-value pairs</strong>.</li><li>Keys must be <strong>unique and hashable</strong>.</li><li>Use .get() when a key might not exist.</li><li>Counting and grouping are two of the most useful dictionary patterns.</li><li>A <strong>list of dictionaries</strong> is a common way to represent rows of structured data.</li><li>Counter and defaultdict can simplify common dictionary-based tasks.</li><li>Practice is the best way to become comfortable with dictionaries.</li></ul><h3>Conclusion</h3><p>Dictionaries aren’t flashy.</p><p>But they’re one of the <strong>quiet workhorses of Python</strong>.</p><p>Once you start thinking in key-value pairs, many problems become much simpler:</p><p><strong>Counting. Grouping. Searching. Organizing. Storing structured data.</strong></p><p>You don’t need to memorize every dictionary method.</p><p>Instead, build something small.</p><p>Create an expense tracker. Make a phone book. Count words. Store student records.</p><p>The more you use dictionaries in real programs, the more naturally you’ll reach for them when solving problems.</p><p>And eventually, you won’t think:</p><blockquote><em>“Should I use a dictionary here?”</em></blockquote><p>You’ll simply know when it’s the right tool.</p><h3>What’s Your Experience With Python Dictionaries?</h3><p>I’d love to hear from you:</p><ul><li>What’s the first project where you used a dictionary?</li><li>Which dictionary mistake has caused you the most debugging trouble?</li><li>Do you prefer using Counter and defaultdict, or do you like writing the logic yourself?</li></ul><p><strong>Share your experience in the comments!</strong></p><p>And if you tried the challenges above, feel free to share your solution too.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9c51d184247" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>