<?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 Sirijaamalapurapu on Medium]]></title>
        <description><![CDATA[Stories by Sirijaamalapurapu on Medium]]></description>
        <link>https://medium.com/@sirijaamalapurapu?source=rss-b515ea7c3072------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Fes8ipgnLo-LCDJO</url>
            <title>Stories by Sirijaamalapurapu on Medium</title>
            <link>https://medium.com/@sirijaamalapurapu?source=rss-b515ea7c3072------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 07:29:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sirijaamalapurapu/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[Sets in Python: Removing Duplicates and Improving Performance]]></title>
            <link>https://medium.com/@sirijaamalapurapu/sets-in-python-removing-duplicates-and-improving-performance-4f6e9b1fc896?source=rss-b515ea7c3072------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f6e9b1fc896</guid>
            <category><![CDATA[set-in-python]]></category>
            <dc:creator><![CDATA[Sirijaamalapurapu]]></dc:creator>
            <pubDate>Sun, 15 Feb 2026 05:21:19 GMT</pubDate>
            <atom:updated>2026-02-15T07:14:09.122Z</atom:updated>
            <content:encoded><![CDATA[<p>In real-world programming, data is rarely clean. Whether you’re working with user registrations, transaction logs, product catalogs, or API responses, duplicates are common. Removing repeated values and performing fast lookups are everyday challenges. Fortunately, Python offers an elegant solution: <strong>sets</strong>.</p><p>Sets are built-in data structures that automatically store unique elements. Though simple, they provide major performance benefits — especially in data cleaning and preprocessing tasks. Let’s explore how sets remove duplicates, why they are faster than lists for certain operations, and how to use them effectively in practical scenarios.</p><h3>What Is a Set in Python?</h3><p>A set is an <strong>unordered collection of unique elements</strong>. Unlike lists, sets do not allow duplicate values.</p><pre>numbers = {1, 2, 3, 4}</pre><p>Key characteristics:</p><ul><li>No duplicate values</li><li>Unordered (no indexing)</li><li>Support mathematical operations (union, intersection, etc.)</li><li>Very fast membership testing</li></ul><p>Because sets are unordered, you cannot access elements by index. However, this trade-off allows Python to optimize sets for speed.</p><h3>Removing Duplicates Easily</h3><p>One of the most practical uses of sets is duplicate removal.</p><pre>names = [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Alice&quot;, &quot;Charlie&quot;, &quot;Bob&quot;]<br>unique_names = set(names)<br>print(unique_names)</pre><p>Output:</p><pre>{&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;}</pre><p>With one line of code, duplicates disappear.</p><p>If you need a list again:</p><pre>unique_names = list(set(names))</pre><h3>Why Sets Are Faster</h3><p>The biggest advantage of sets is performance. Internally, Python implements sets using <strong>hash tables</strong>, allowing operations like membership checks, adding, and removing elements to run in average <strong>O(1)</strong> time complexity.</p><h3>Membership in a List (O(n))</h3><pre>large_list = list(range(1_000_000))<br>999999 in large_list</pre><p>hon may scan through many elements before finding the value.</p><h3>Membership in a Set (O(1))</h3><pre>large_set = set(range(1_000_000))<br>999999 in large_set</pre><p>This lookup is almost instantaneous, even with one million elements.</p><p>For large datasets or frequent lookups, switching from a list to a set can dramatically improve performance.</p><h3>Powerful Set Operations</h3><p>Sets support mathematical operations that make comparing datasets simple and efficient.</p><h3>1. Union (Combine Unique Values)</h3><pre>a = {1, 2, 3}<br>b = {3, 4, 5}<br>print(a | b)</pre><p>Output:</p><pre>{1, 2, 3, 4, 5}</pre><p>Useful when merging data from multiple sources.</p><h3>2. Intersection (Common Elements)</h3><pre>print(a &amp; b)</pre><p>Output:</p><pre>{3}</pre><p>Helpful for identifying overlapping records between systems.</p><h3>3. Difference (What’s Missing?)</h3><pre>print(a - b)</pre><p>Output:</p><pre>{1, 2}</pre><p>Great for detecting missing records.</p><h3>4. Symmetric Difference (What’s Different?)</h3><pre>print(a ^ b)</pre><p>Output:</p><pre>{1, 2, 4, 5}</pre><p>Useful when comparing two datasets to find differences.</p><h3>Practical Real-World Examples</h3><h3>Cleaning Email Lists</h3><pre>emails = [<br>    &quot;user1@gmail.com&quot;,<br>    &quot;user2@gmail.com&quot;,<br>    &quot;user1@gmail.com&quot;,<br>    &quot;user3@gmail.com&quot;<br>]</pre><pre>cleaned_emails = set(emails)</pre><p>Duplicates are removed instantly — important before sending marketing emails.</p><h3>Finding Common Users</h3><pre>system1_users = {&quot;alice&quot;, &quot;bob&quot;, &quot;charlie&quot;}<br>system2_users = {&quot;bob&quot;, &quot;david&quot;, &quot;charlie&quot;}</pre><pre>common_users = system1_users &amp; system2_users</pre><p>This helps during system migrations or integrations.</p><h3>Detecting Missing Records</h3><pre>all_ids = {1, 2, 3, 4, 5}<br>processed_ids = {2, 3, 5}</pre><pre>missing_ids = all_ids - processed_ids</pre><p>Often used in financial systems or data pipelines to ensure completeness.</p><h3>Detecting Duplicates While Iterating</h3><pre>seen = set()<br>duplicates = []</pre><pre>for item in [&quot;a&quot;, &quot;b&quot;, &quot;a&quot;, &quot;c&quot;, &quot;b&quot;]:<br>    if item in seen:<br>        duplicates.append(item)<br>    else:<br>        seen.add(item)</pre><pre>print(duplicates)</pre><p>This method lets you track duplicates without converting the entire list at once.</p><h3>When Not to Use Sets</h3><p>Sets are powerful, but not always ideal.</p><p>Avoid them when:</p><ul><li>Order matters</li><li>You need to preserve duplicates</li><li>Elements are mutable (like lists or dictionaries)</li></ul><p>Example:</p><pre>invalid_set = {[1, 2], [3, 4]}</pre><p>This raises an error because lists are mutable and cannot be stored in a set.</p><h3>Conclusion</h3><p>Sets in Python are highly efficient. They remove duplicates automatically, perform lightning-fast membership checks, and make dataset comparisons easy through mathematical operations.</p><p>Whether you’re cleaning customer data, comparing logs, detecting missing records, or optimizing performance, sets provide a clean and powerful solution. As datasets grow larger, choosing the right data structure becomes critical — and in many cases, switching from a list to a set can transform a slow approach into an efficient one.</p><p>If you want to write cleaner and faster Python code, mastering sets is an essential step forward.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f6e9b1fc896" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>