Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Set in Python

Introduction

A set in Python is an unordered, mutable, and unindexed collection of unique elements. Sets are useful when you need to store distinct values and perform mathematical set operations like union, intersection, and difference.


Characteristics of Sets in Python

  • Unordered: Elements do not have a fixed position.
  • Mutable: Elements can be added or removed.
  • Unindexed: Cannot be accessed using an index.
  • Unique Elements: No duplicates allowed.
  • Heterogeneous Elements: Can store different data types.

Workflow of Set Operations

  1. Create a Set
  2. Add elements using add() or update()
  3. Remove elements using remove(), discard(), or pop()
  4. Perform set operations like union, intersection, and difference
  5. Check relationships using issubset(), issuperset(), and isdisjoint()
  6. Use frozen sets for immutable sets if needed

Creating a Set

# Using curly braces
my_set = {1, 2, 3, 4, 5}

# Using set() constructor
another_set = set([10, 20, 30])

# Creating an empty set
empty_set = set()

Adding Elements to a Set

my_set = {1, 2, 3}

# Add a single element
my_set.add(4)

# Add multiple elements
my_set.update([5, 6, 7])

Removing Elements from a Set

my_set = {1, 2, 3, 4, 5}

# Remove an element
my_set.remove(3)  # Raises error if element is absent

# Discard an element
my_set.discard(2)  # No error if element is absent

# Remove and return a random element
my_set.pop()

# Clear all elements
my_set.clear()

Set Operations

Union

A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)  # {1, 2, 3, 4, 5}

Intersection

print(A & B)  # {3}

Difference

print(A - B)  # {1, 2}

Symmetric Difference

print(A ^ B)  # {1, 2, 4, 5}

Checking Set Relations

A = {1, 2}
B = {1, 2, 3, 4}

print(A.issubset(B))  # True
print(B.issuperset(A))  # True
print(A.isdisjoint({5, 6}))  # True

Frozen Set (Immutable Set)

A = frozenset([1, 2, 3, 4])
# A.add(5)  # Error: 'frozenset' object has no attribute 'add'

Practical Use Cases

  • Removing duplicates from a list
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
  • Finding common elements between lists
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common = set(list1) & set(list2)
  • Set operations in data analysis
students_A = {"Alice", "Bob", "Charlie"}
students_B = {"Charlie", "David", "Edward"}
both_classes = students_A | students_B