Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

📚 What is a Set Data Structure?

A set is an unordered collection of unique and immutable elements in Python. It is defined by placing elements within curly braces {} or by using the built-in set() function.

Key Characteristics of a Set:

  • Unordered: The elements do not have a defined order.
  • Unique: No duplicate elements are allowed.
  • Mutable: You can add or remove elements from a set.

🧰 Where Do We Use Sets in Python?

Sets are used in Python for various tasks that require handling unique items and performing operations like:

  • 🔍 Removing duplicates from a list.
  • Checking membership (whether an element is in a set).
  • Performing mathematical operations like union, intersection, difference, and symmetric difference.

🔍 Why Do We Need Data Structures Like Sets?

  1. Uniqueness: Sets automatically handle uniqueness, making them perfect for filtering duplicates.
  2. Efficient Membership Testing: Sets provide O(1) average-time complexity for membership tests.
  3. Mathematical Operations: Sets are ideal for performing union, intersection, and difference operations.

🔧 Set Initialization Examples

Example 1: Creating a Set Using Curly Braces

# Creating a set with curly braces
my_set = {1, 2, 3, 4, 5}
print(my_set)  # Output: {1, 2, 3, 4, 5}

Example 2: Creating a Set Using the set() Constructor

# Creating a set from a list using the set() constructor
my_set = set([1, 2, 3, 3, 4, 4, 5])
print(my_set)  # Output: {1, 2, 3, 4, 5}

Example 3: Creating an Empty Set

# Creating an empty set
empty_set = set()
print(empty_set)  # Output: set()

🛠️ Set Methods with Examples

Python provides several built-in methods for sets. Here’s a detailed list of all set methods with examples:

1. add(): Adds an element to the set.

# 🍎 Adding an element to the set
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)  # Output: {'apple', 'banana', 'cherry', 'orange'}
  • 📝 Explanation: The add() method is used to add a single element to a set. Since sets do not allow duplicates, if the element already exists, it will not be added again.

2. clear(): Removes all elements from the set.

# 🧹 Clearing all elements from the set
fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits)  # Output: set()
  • 📝 Explanation: The clear() method removes all the elements from the set, resulting in an empty set.

3. copy(): Returns a shallow copy of the set.

# 📋 Making a shallow copy of the set
fruits = {"apple", "banana", "cherry"}
new_fruits = fruits.copy()
print(new_fruits)  # Output: {'apple', 'banana', 'cherry'}
  • 📝 Explanation: The copy() method creates a shallow copy of the set. This means a new set is created, but the elements are references to the same objects in memory.

What is a Shallow Copy?

A shallow copy creates a new set object but does not create copies of the elements contained in the original set; instead, it references them. If the elements are immutable (like strings, integers), a shallow copy behaves the same as a deep copy. However, if the set contains mutable objects (like lists or dictionaries), changes to those mutable objects will be reflected in both the original and the copied set.

4. difference(): Returns a set containing the difference between two or more sets.

# ➖ Difference between sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.difference(set2)
print(result)  # Output: {1, 2}
  • 📝 Explanation: The difference() method returns a new set containing elements that are in the first set but not in the second set.

5. difference_update(): Removes the items in this set that are also included in another specified set.

# 🔄 Updating set1 by removing common elements
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set1.difference_update(set2)
print(set1)  # Output: {1, 2}
  • 📝 Explanation: The difference_update() method removes all elements of the set that are also present in another specified set.

6. discard(): Removes the specified item. If the item is not found, it does not raise an error.

# 🚫 Discarding an element
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)  # Output: {'apple', 'cherry'}
fruits.discard("pear")  # Does nothing since "pear" is not in the set
  • 📝 Explanation: The discard() method removes the specified element from the set without raising an error if the element is not found.

7. intersection(): Returns a set that is the intersection of two or more sets.

# ⛓️ Intersection of sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.intersection(set2)
print(result)  # Output: {3, 4}
  • 📝 Explanation: The intersection() method returns a new set that contains only the common elements from both sets.

8. intersection_update(): Updates the set with the intersection of itself and another.

# 🔄 Updating set1 to its intersection with set2
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set1.intersection_update(set2)
print(set1)  # Output: {3, 4}
  • 📝 Explanation: The intersection_update() method updates the set with the intersection of itself and another set.

9. isdisjoint(): Returns True if two sets have a null intersection.

# ❌ Checking if sets are disjoint
set1 = {1, 2, 3}
set2 = {4, 5, 6}
print(set1.isdisjoint(set2))  # Output: True
  • 📝 Explanation: The isdisjoint() method returns True if two sets have no elements in common.

10. issubset(): Returns True if all elements of a set are present in another set.

# ✔️ Checking if set1 is a subset of set2
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
print(set1.issubset(set2))  # Output: True
  • 📝 Explanation: The issubset() method checks if all elements of the set are present in another set.

11. issuperset(): Returns True if all elements of another set are present in this set.

# ✔️ Checking if set1 is a superset of set2
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
print(set1.issuperset(set2))  # Output: True
  • 📝 Explanation: The issuperset() method checks if the set contains all elements of another set.

12. pop(): Removes and returns an arbitrary element from the set.

# 🔄 Popping an element from the set
fruits = {"apple", "banana", "cherry"}
item = fruits.pop()
print(item)  # Output: Random element (e.g., 'banana')
print(fruits)  # Output: Remaining set without

 the popped element
  • 📝 Explanation: The pop() method removes and returns a random element from the set. Since sets are unordered, there is no guarantee which element will be removed.

13. remove(): Removes the specified element from the set. If the element is not found, it raises a KeyError.

# 🗑️ Removing an element from the set
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)  # Output: {'apple', 'cherry'}
# fruits.remove("pear")  # Raises KeyError
  • 📝 Explanation: The remove() method removes the specified element from the set. If the element is not present, it raises a KeyError.

14. symmetric_difference(): Returns a set with elements in either the set or the specified set, but not in both.

# 🔄 Symmetric difference between sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.symmetric_difference(set2)
print(result)  # Output: {1, 2, 4, 5}
  • 📝 Explanation: The symmetric_difference() method returns a set containing elements that are in either set but not in both.

15. symmetric_difference_update(): Updates the set with the symmetric difference of itself and another.

# 🔄 Updating set1 to its symmetric difference with set2
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.symmetric_difference_update(set2)
print(set1)  # Output: {1, 2, 4, 5}
  • 📝 Explanation: The symmetric_difference_update() method updates the set with the symmetric difference of itself and another set.

16. union(): Returns a set that contains all items from the original set and all items from the specified sets.

# 🔗 Union of two sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2)
print(result)  # Output: {1, 2, 3, 4, 5}
  • 📝 Explanation: The union() method returns a new set containing all the unique elements from both sets.

17. update(): Updates the set with the union of itself and others.

# 🔄 Updating set1 with union of set2
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2)
print(set1)  # Output: {1, 2, 3, 4, 5}
  • 📝 Explanation: The update() method adds all elements from another set (or any iterable) to the set.

🧑‍💻 Conclusion

Sets are powerful data structures that provide an efficient way to handle collections of unique items. They are particularly useful in situations where duplicates need to be avoided, and various mathematical set operations are required. With this comprehensive guide and examples, you now have a solid understanding of Python sets, their capabilities, and how shallow copies work.