Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Python Set Essentials

Tech 1

Python Sets

In Python, a set is an unordered collection of unique elements. It is primarily used for mathematical set operations like union, intersection, difference, and symmetric difference. Sets are defined using curly braces {}, but note that you cannot include mutable types (e.g., lists) as elements, and an empty pair of braces {} is interpreted as an empty dictionary, not a set. To create an empty set, you must use set().

Defining Sets

You can define a set directly using curly braces (remember: no mutable elements and no empty braces for a set):

my_set = {1, 2, 3, 4, 5}  # A set of integers
# Caution: {} creates an empty dictionary, not an empty set

Using the set() function:

my_set = set([1, 2, 2, 3, 4])  # Duplicates are automatically removed
empty_set = set()              # This creates an empty set

Set Characteristics

Sets are unordered and do not support indexing. You cannot access elements by position:

s = {1, 2, 3, 4}
for i in range(len(s)):
    print(i, s[i])  # Error: 'set' object is not subscriptable

Attempting to index a set causes an error

Sets contain no duplicates, making them useful for removing duplicates from a collection:

s = {1, 2, 3, 4}
s2 = {1, 3, 7, 9, 11}
s.update(s2)
print(s)  # Output could be {1, 2, 3, 4, 7, 9, 11} (order may vary)

Result of union update

When a set contains numbers, they may appear sorted in ascending order when printed (but this is an implementation detail, not guaranteed):

s = {1, 2, 3, 4, 7, 6, 5, 9, 11}
print(s)  # May display {1, 2, 3, 4, 5, 6, 7, 9, 11}

Set printed in ascending order

Sets are mutable; you can add or remove elements:

s = {1, 2, 3, 4, 5, 6, 7}
s.add(9)
print(s)  # Output: {1, 2, 3, 4, 5, 6, 7, 9}

Set after adding element

s = {1, 2, 3, 4, 5, 6, 7}
s.remove(5)
print(s)  # Output: {1, 2, 3, 4, 6, 7}

Set after removing element

Mathematical Set Opertaions

  • Union (| or union()): elements in either set.
  • Intersection (& or intersection()): elemants common to both sets.
  • Difference (- or difference()): elements in the first set but not in the second.
  • Symmetric difference (^ or symmetric_difference()): elements in exactly one of the sets.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

print(set1 | set2)  # Union: {1, 2, 3, 4, 5, 6}
print(set1 & set2)  # Intersection: {3, 4}
print(set1 - set2)  # Difference: {1, 2}
print(set1 ^ set2)  # Symmetric difference: {1, 2, 5, 6}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.