<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohammad Nadeem</title>
    <description>The latest articles on DEV Community by Mohammad Nadeem (@techienadeem).</description>
    <link>https://dev.to/techienadeem</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3338302%2F19ecbb6b-f555-42e9-a359-fb8e3425763f.png</url>
      <title>DEV Community: Mohammad Nadeem</title>
      <link>https://dev.to/techienadeem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/techienadeem"/>
    <language>en</language>
    <item>
      <title>🛠️ Real-World Use Cases of Sets and Dicts in APIs &amp; Data Processing</title>
      <dc:creator>Mohammad Nadeem</dc:creator>
      <pubDate>Wed, 09 Jul 2025 11:32:55 +0000</pubDate>
      <link>https://dev.to/techienadeem/real-world-use-cases-of-sets-and-dicts-in-apis-data-processing-1224</link>
      <guid>https://dev.to/techienadeem/real-world-use-cases-of-sets-and-dicts-in-apis-data-processing-1224</guid>
      <description>&lt;p&gt;You’ve learned about sets and dictionaries — but where do they shine in &lt;strong&gt;real-world Python projects&lt;/strong&gt;, especially in &lt;strong&gt;APIs and data pipelines&lt;/strong&gt;? Let’s look at practical, production-friendly use cases.&lt;/p&gt;




&lt;p&gt;🚀 &lt;strong&gt;1. Fast Duplicate Removal with Sets&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;✅ Example: Cleaning up email list before sending campaign&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;emails = ['alice@example.com', 'bob@example.com', 'alice@example.com']
unique_emails = list(set(emails))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 &lt;strong&gt;2. Efficient Membership Checks&lt;/strong&gt;&lt;br&gt;
✅ Example: Check if a user has access to a resource&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;authorized_user_ids = {1001, 1002, 1003}
if user_id in authorized_user_ids:
    return "Access granted"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Sets make this check O(1) — way faster than a list.&lt;/p&gt;

&lt;p&gt;🔁 &lt;strong&gt;3. Grouping API Records with Dictionaries&lt;/strong&gt;&lt;br&gt;
✅ Example: Group transactions by user ID&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from collections import defaultdict

transactions = [
    {'user_id': 1, 'amount': 100},
    {'user_id': 2, 'amount': 50},
    {'user_id': 1, 'amount': 70}
]

grouped = defaultdict(list)
for txn in transactions:
    grouped[txn['user_id']].append(txn)

# grouped = {1: [...], 2: [...]}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔄 &lt;strong&gt;4. Remapping or Normalizing API Payloads&lt;/strong&gt;&lt;br&gt;
✅ Example: Renaming keys from snake_case to camelCase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def to_camel_case(snake_str):
    parts = snake_str.split('_')
    return parts[0] + ''.join(x.title() for x in parts[1:])

payload = {'user_id': 123, 'user_name': 'alice'}
normalized = {to_camel_case(k): v for k, v in payload.items()}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔀 &lt;strong&gt;5. Mapping IDs to Names for API Response Enrichment&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_map = {1: 'Alice', 2: 'Bob'}
transactions = [{'user_id': 1, 'amount': 50}, {'user_id': 2, 'amount': 100}]

for txn in transactions:
    txn['user_name'] = user_map.get(txn['user_id'], 'Unknown')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;6. Detecting Shared Items Between Datasets&lt;/strong&gt;&lt;br&gt;
✅ Example: Users active in both services&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service_a_users = {1, 2, 3}
service_b_users = {3, 4, 5}

both = service_a_users &amp;amp; service_b_users  # {3}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✨ &lt;strong&gt;Summary&lt;/strong&gt;
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Tool Used&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Remove duplicates&lt;/td&gt;
&lt;td&gt;&lt;code&gt;set()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fast lookup&lt;/td&gt;
&lt;td&gt;&lt;code&gt;set&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Group by field&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;dict&lt;/code&gt;, &lt;code&gt;defaultdict&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rename JSON keys&lt;/td&gt;
&lt;td&gt;Dictionary comprehension&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Enrich API payloads&lt;/td&gt;
&lt;td&gt;&lt;code&gt;dict.get()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Find common users/items&lt;/td&gt;
&lt;td&gt;Set intersection (&lt;code&gt;&amp;amp;&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Sets and dictionaries are workhorses in backend engineering, especially when building scalable APIs or ETL systems. Use them wisely to improve performance, readability, and efficiency.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Follow for more real-world backend Python tips 🐍⚙️&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Dictionary Comprehensions in Python: Powerful, Elegant, and Fast</title>
      <dc:creator>Mohammad Nadeem</dc:creator>
      <pubDate>Wed, 09 Jul 2025 11:24:17 +0000</pubDate>
      <link>https://dev.to/techienadeem/dictionary-comprehensions-in-python-powerful-elegant-and-fast-3434</link>
      <guid>https://dev.to/techienadeem/dictionary-comprehensions-in-python-powerful-elegant-and-fast-3434</guid>
      <description>&lt;p&gt;&lt;em&gt;If you love list comprehensions in Python, you're going to love dictionary comprehensions too. They're concise, expressive, and often much faster than traditional for-loops.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  📘 &lt;strong&gt;What Is a Dictionary Comprehension?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;A &lt;strong&gt;dictionary comprehension&lt;/strong&gt; is a concise way to create dictionaries using an expression inside curly braces.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{key_expr: value_expr for item in iterable}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Basic Example&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Let’s create a dictionary where keys are numbers and values are their squares:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;squares = {x: x**2 for x in range(5)}
print(squares)  # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔄 &lt;strong&gt;Reversing a Dictionary&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;original = {'a': 1, 'b': 2, 'c': 3}
reversed_dict = {v: k for k, v in original.items()}
print(reversed_dict)  # {1: 'a', 2: 'b', 3: 'c'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧼 &lt;strong&gt;Filtering with Conditions&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Only include even keys:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;even_squares = {x: x**2 for x in range(10) if x % 2 == 0}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📦 &lt;strong&gt;Nested Dictionary Comprehensions&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Creating a grid (2D dict):&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grid = {(x, y): x*y for x in range(3) for y in range(3)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 &lt;strong&gt;Use Case: Rename JSON Keys&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;api_data = {'user_id': 1, 'user_name': 'alice'}
mapped = {k.replace('user_', ''): v for k, v in api_data.items()}
# {'id': 1, 'name': 'alice'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⏱️ &lt;strong&gt;Why Use Them?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cleaner and more expressive&lt;/li&gt;
&lt;li&gt;Often faster than loops&lt;/li&gt;
&lt;li&gt;Ideal for transformations, filters, or small data manipulations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🧪 &lt;strong&gt;Pro Tip&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use dictionary comprehensions when:&lt;/li&gt;
&lt;li&gt;You’re transforming or filtering a dictionary&lt;/li&gt;
&lt;li&gt;You’re building one from another iterable&lt;/li&gt;
&lt;li&gt;You want to keep your code elegant and Pythonic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Follow for more Python deep dives! 🐍⚡&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🧠 Advanced Set Operations in Python — Beyond the Basics</title>
      <dc:creator>Mohammad Nadeem</dc:creator>
      <pubDate>Wed, 09 Jul 2025 11:14:05 +0000</pubDate>
      <link>https://dev.to/techienadeem/advanced-set-operations-in-python-beyond-the-basics-3n47</link>
      <guid>https://dev.to/techienadeem/advanced-set-operations-in-python-beyond-the-basics-3n47</guid>
      <description>&lt;p&gt;&lt;em&gt;You probably know that Python sets are great for storing unique values and checking membership fast. But sets are far more powerful when you start using advanced operations like unions, intersections, symmetric differences, and more. Let's dive deep into what makes sets an underrated gem in Python's standard library.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;📦 &lt;strong&gt;Quick Refresher: What is a Set?&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;A set is an unordered, mutable, and duplicate-free collection:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = {1, 2, 3}
b = set([3, 4, 5])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sets are perfect when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to remove duplicates&lt;/li&gt;
&lt;li&gt;You care about fast membership checks&lt;/li&gt;
&lt;li&gt;You want to compare data collections&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔄 &lt;strong&gt;1. Union: | or .union()&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Combine elements from both sets — no duplicates.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)  # {1, 2, 3, 4, 5}

Or:

a.union(b)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔁 &lt;strong&gt;2. Intersection: &amp;amp; or .intersection()&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Get common elements between sets.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(a &amp;amp; b)  # {3}

Or:

a.intersection(b)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔀 &lt;strong&gt;3. Difference: - or .difference()&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Get items in a but not in b.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(a - b)  # {1, 2}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is order-sensitive: a - b ≠ b - a.&lt;/p&gt;

&lt;p&gt;✖️ &lt;strong&gt;4. Symmetric Difference: ^ or .symmetric_difference()&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Elements that are in either set but not both.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(a ^ b)  # {1, 2, 4, 5}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;5. Subset and Superset&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = {1, 2}
b = {1, 2, 3, 4}

print(a &amp;lt;= b)  # True (a is subset of b)
print(b &amp;gt;= a)  # True (b is superset of a)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a.issubset(b)
b.issuperset(a)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🚫 &lt;strong&gt;6. Disjoint Sets&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Check if two sets have no elements in common.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = {1, 2}
b = {3, 4}
print(a.isdisjoint(b))  # True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚙️ &lt;strong&gt;7. Set Comprehensions&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Build sets on the fly:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;squares = {x * x for x in range(6)}
print(squares)  # {0, 1, 4, 9, 16, 25}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Filter during construction:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;evens = {x for x in range(10) if x % 2 == 0}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💥 &lt;strong&gt;8. Updating Sets In-Place&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Mutating operations:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = {1, 2, 3}
b = {3, 4, 5}

a.update(b)       # Like union
a.intersection_update({2, 3, 4})  # Keep only shared items
a.difference_update({3})         # Remove common items
a.symmetric_difference_update({2, 5})  # Replace with symmetric diff

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧪 &lt;strong&gt;Real-World Example: Finding Unique vs Shared Users&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;logged_in_users = {'alice', 'bob', 'carol'}
purchased_users = {'carol', 'dave'}

# Who logged in but didn't purchase
print(logged_in_users - purchased_users)  # {'alice', 'bob'}

# Who both logged in and purchased
print(logged_in_users &amp;amp; purchased_users)  # {'carol'}

# Everyone involved
print(logged_in_users | purchased_users)  # {'alice', 'bob', 'carol', 'dave'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 &lt;strong&gt;Pro Tips&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sets are faster than lists for membership checks (in).&lt;/li&gt;
&lt;li&gt;Use frozenset() when you need an immutable set (e.g., as a dict key).&lt;/li&gt;
&lt;li&gt;Avoid using sets when order matters (use list or OrderedDict instead).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🧠 Summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Symbol&lt;/th&gt;
&lt;th&gt;Method Equivalent&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Union&lt;/td&gt;
&lt;td&gt;a | b&lt;/td&gt;
&lt;td&gt;a.union(b)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Intersection&lt;/td&gt;
&lt;td&gt;a &amp;amp; b&lt;/td&gt;
&lt;td&gt;a.intersection(b)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Difference&lt;/td&gt;
&lt;td&gt;a - b&lt;/td&gt;
&lt;td&gt;a.difference(b)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Symmetric Difference&lt;/td&gt;
&lt;td&gt;a ^ b&lt;/td&gt;
&lt;td&gt;a.symmetric_difference(b)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subset / Superset&lt;/td&gt;
&lt;td&gt;a &amp;lt;= b, a &amp;gt;= b&lt;/td&gt;
&lt;td&gt;a.issubset(b), a.issuperset(b)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Disjoint&lt;/td&gt;
&lt;td&gt;&lt;em&gt;(no symbol)&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;a.isdisjoint(b)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;🏁 &lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Python sets are simple but powerful tools for data comparison, filtering, and logic. If you're writing code that needs to compare or filter collections — chances are, sets will make your life easier and your code cleaner.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🗨️ &lt;strong&gt;What’s Next?&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Want to explore more?&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using Counter for multiset behavior&lt;/li&gt;
&lt;li&gt;Building set-based recommendation systems&lt;/li&gt;
&lt;li&gt;Performance benchmarking: list vs set lookup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Follow for more Python deep dives! 🐍⚡&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Custom Sorting in Python: Lists and Dictionaries Demystified</title>
      <dc:creator>Mohammad Nadeem</dc:creator>
      <pubDate>Wed, 09 Jul 2025 10:49:07 +0000</pubDate>
      <link>https://dev.to/techienadeem/custom-sorting-in-python-lists-and-dictionaries-demystified-44be</link>
      <guid>https://dev.to/techienadeem/custom-sorting-in-python-lists-and-dictionaries-demystified-44be</guid>
      <description>&lt;p&gt;&lt;em&gt;Sorting data in Python is easy with sorted(), but what if you want to sort by custom logic? In this post, we’ll explore how to sort lists and dictionaries in Python using custom keys and lambda functions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🔢 &lt;strong&gt;Sorting Lists in Python&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Default List Sorting&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [5, 2, 9, 1]
print(sorted(numbers))  # [1, 2, 5, 9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This works great for simple data. But what about sorting by length, reverse order, or object properties?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🧠 &lt;strong&gt;Custom Sorting with key=&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Sort strings by length&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;words = ['banana', 'apple', 'kiwi', 'strawberry']
sorted_by_length = sorted(words, key=len)
print(sorted_by_length)
# ['kiwi', 'apple', 'banana', 'strawberry']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Sort strings by last character&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sorted_by_last_char = sorted(words, key=lambda x: x[-1])
print(sorted_by_last_char)
# ['banana', 'kiwi', 'apple', 'strawberry']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Sort a list of tuples by second value&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pairs = [(1, 3), (2, 2), (4, 1)]
sorted_pairs = sorted(pairs, key=lambda x: x[1])
print(sorted_pairs)
# [(4, 1), (2, 2), (1, 3)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧰 &lt;strong&gt;Sorting Lists of Dicts&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;This is common in APIs or tabular data:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;users = [
    {'name': 'Alice', 'age': 30},
    {'name': 'Bob', 'age': 25},
    {'name': 'Charlie', 'age': 35}
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔽 &lt;strong&gt;Sort by age:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sorted_users = sorted(users, key=lambda x: x['age'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔼 &lt;strong&gt;Sort by name (descending):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sorted_users = sorted(users, key=lambda x: x['name'], reverse=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 &lt;strong&gt;Sorting Dictionaries by Keys or Values&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;By default, dictionaries in Python 3.7+ maintain insertion order. But we can sort them for display or processing.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scores = {'Alice': 82, 'Bob': 91, 'Charlie': 78}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔤 &lt;strong&gt;Sort by key:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sorted_by_key = dict(sorted(scores.items()))
# {'Alice': 82, 'Bob': 91, 'Charlie': 78}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔢 &lt;strong&gt;Sort by value:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sorted_by_value = dict(sorted(scores.items(), key=lambda item: item[1]))
# {'Charlie': 78, 'Alice': 82, 'Bob': 91}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔄 &lt;strong&gt;In-Place Sorting with .sort()&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;If you want to sort a list in-place (not return a new one):&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [4, 1, 3]
numbers.sort()
print(numbers)  # [1, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;With custom key:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;words.sort(key=lambda x: len(x))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚠️ .sort() works only on lists, not on dict or tuple.&lt;/p&gt;

&lt;p&gt;⚙️ &lt;strong&gt;Pro Tip:&lt;/strong&gt; &lt;em&gt;Use operator.itemgetter for cleaner syntax&lt;br&gt;
from operator import itemgetter&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sorted_users = sorted(users, key=itemgetter('age'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Works well when sorting by multiple keys too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sorted_users = sorted(users, key=itemgetter('age', 'name'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✨ &lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Mastering custom sorting in Python helps you handle data more efficiently — especially when working with APIs, CSVs, or databases. Whether you’re sorting by value, custom rules, or multiple fields, Python gives you all the tools you need.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🚀 &lt;strong&gt;Next up?&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;In the next post, I’ll dive into:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sorting with multiple criteria&lt;/li&gt;
&lt;li&gt;Using functools.cmp_to_key&lt;/li&gt;
&lt;li&gt;Performance tips when sorting large datasets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Follow for more Python tricks, clean code tips, and backend dev guides!&lt;/em&gt; 🐍💻&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Mastering Python Collections: List, Tuple, Set, and Dictionary</title>
      <dc:creator>Mohammad Nadeem</dc:creator>
      <pubDate>Wed, 09 Jul 2025 10:31:53 +0000</pubDate>
      <link>https://dev.to/techienadeem/mastering-python-collections-list-tuple-set-and-dictionary-3p7f</link>
      <guid>https://dev.to/techienadeem/mastering-python-collections-list-tuple-set-and-dictionary-3p7f</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. Python Lists – The Ordered All-Rounder&lt;/strong&gt;&lt;br&gt;
A list is a mutable, ordered collection that can store items of different types.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ordered&lt;/li&gt;
&lt;li&gt;Mutable (can change)&lt;/li&gt;
&lt;li&gt;Allows duplicates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Syntax &amp;amp; Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits = ['apple', 'banana', 'cherry']
fruits.append('mango')
print(fruits)  # ['apple', 'banana', 'cherry', 'mango']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔧 &lt;strong&gt;Common List Operations:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits[0]         # Access first item → 'apple'
fruits[-1]        # Last item → 'mango'
len(fruits)       # 4
'banana' in fruits  # True
fruits.remove('apple')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔒 &lt;strong&gt;2. Python Tuples – The Immutable Sibling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A tuple is like a list, but immutable (cannot be changed after creation).&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ordered&lt;/li&gt;
&lt;li&gt;Immutable&lt;/li&gt;
&lt;li&gt;Allows duplicates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Syntax &amp;amp; Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;person = ('Alice', 30, 'Engineer')
print(person[1])  # 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚠️ &lt;strong&gt;Single-Element Tuples:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;t = ('hello',)  # With comma — valid tuple
not_a_tuple = ('hello')  # Just a string!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fixed data structures (e.g., coordinates, DB rows)&lt;/li&gt;
&lt;li&gt;As keys in dictionaries (if they contain only immutable types)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🧹 &lt;strong&gt;3. Python Sets – The Unique Collection&lt;/strong&gt;&lt;br&gt;
A set is an unordered, mutable collection of unique items.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unordered&lt;/li&gt;
&lt;li&gt;No duplicates&lt;/li&gt;
&lt;li&gt;Fast membership tests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Syntax &amp;amp; Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors = {'red', 'green', 'blue'}
colors.add('yellow')
colors.add('red')  # Ignored, already exists
print(colors)  # {'green', 'blue', 'red', 'yellow'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔧 &lt;strong&gt;Common Set Operations:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors.remove('blue')
'aqua' in colors         # False
colors.union({'black'})  # New set with combined items
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚠️ &lt;strong&gt;Empty set:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = set()     # ✅ This is an empty set
s2 = {}       # ⚠️ This is an empty dictionary!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 &lt;strong&gt;4. Python Dictionaries – The Key-Value Store&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A dictionary (or dict) stores data as key-value pairs.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unordered (in &amp;lt; Python 3.7)&lt;/li&gt;
&lt;li&gt;Keys must be unique and immutable&lt;/li&gt;
&lt;li&gt;Values can be of any type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 &lt;strong&gt;Syntax &amp;amp; Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user = {
    'name': 'John',
    'age': 25,
    'email': 'john@example.com'
}
print(user['name'])  # John
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔧 &lt;strong&gt;Common Dictionary Operations:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user['age'] = 26         # Update
user.get('city', 'N/A')  # Safe lookup → 'N/A'
del user['email']        # Remove key
'user' in user           # False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Nested Dict Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;employee = {
    'id': 101,
    'profile': {'name': 'Jane', 'role': 'Manager'}
}
print(employee['profile']['name'])  # Jane
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✨ &lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;br&gt;
Understanding these core Python data structures is essential for effective programming. Choose the right one based on your needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use lists for general-purpose ordered data&lt;/li&gt;
&lt;li&gt;Use tuples for fixed-size, immutable groups&lt;/li&gt;
&lt;li&gt;Use sets for fast membership checks and unique values&lt;/li&gt;
&lt;li&gt;Use dictionaries when you need to map keys to values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🗨️ &lt;strong&gt;What’s Next?&lt;/strong&gt;&lt;br&gt;
Want to go deeper? In future posts, I’ll cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Custom sorting of lists/dicts&lt;/li&gt;
&lt;li&gt;Advanced set operations&lt;/li&gt;
&lt;li&gt;Dictionary comprehensions&lt;/li&gt;
&lt;li&gt;Real-world use cases in APIs and data processing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Follow me for more Python tips and tricks! 🐍✨&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
