PYTHON

Set Datatype


Set is homogenous and heterogeneous collection of elements.

  • Elements must exist in real, and it enclosed wit { } boundary. 
  • Set is mutable type data, that is we can modify original set.
  • Set elements are immutable and separated by ‘ , ’ .
  • Set is unique data type, never allows duplicate elements.
  • Set is unordered, non-sequential, hence position of the elements is not fixed.
  • Indexing and Slicing is not possible.

 

Creating Set

Sets can be created by placing a comma-separated sequence of elements within curly braces {} or by using the built-in set() function.

Examples:

# Creating a set with curly braces
fruits = {"apple", "banana", "cherry"}
# Creating a set with the set() function
numbers = set([1, 2, 3, 4, 5])
# Creating an empty set
empty_set = set()  # Note: {} creates an empty dictionary, not a set

 

 

Adding Elements

You can add elements to a set using the add() method or the update() method to add multiple elements.

Examples:

fruits = {"apple", "banana", "cherry"}
# Adding a single element
fruits.add("orange")
print(fruits)  # Output: {'banana', 'apple', 'cherry', 'orange'}
# Adding multiple elements
fruits.update(["kiwi", "mango"])
print(fruits)  # Output: {'banana', 'apple', 'cherry', 'kiwi', 'orange', 'mango'}
 

 

Removing Elements

You can remove elements from a set using methods like remove(), discard(), and pop().

Examples:

fruits = {"apple", "banana", "cherry"}
# Removing an element with remove() (raises an error if the element is not present)
fruits.remove("banana")
print(fruits)  # Output: {'apple', 'cherry'}
# Removing an element with discard() (does not raise an error if the element is not present)
fruits.discard("kiwi")
print(fruits)  # Output: {'apple', 'cherry'}
# Removing and returning an arbitrary element with pop()
popped_element = fruits.pop()
print(popped_element)  # Output: apple (or cherry, as the set is unordered)
print(fruits)          # Output: {'cherry'} (or {'apple'})

 

 

Set Operations

Python provides several methods and operators for performing set operations like union, intersection, difference, and symmetric difference.

Union: Combines all elements from both sets (duplicates are removed).

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)  # Output: {1, 2, 3, 4, 5}
# Using the | operator
union_set = set1 | set2
print(union_set)  # Output: {1, 2, 3, 4, 5}

 

 

Intersection: Returns elements that are common to both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {3}
# Using the & operator
intersection_set = set1 & set2
print(intersection_set)  # Output: {3}

 

 

Difference: Returns elements that are in the first set but not in the second.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print(difference_set)  # Output: {1, 2}
# Using the - operator
difference_set = set1 - set2
print(difference_set)  # Output: {1, 2}

 

 

Symmetric Difference: Returns elements that are in either of the sets, but not in both.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
sym_diff_set = set1.symmetric_difference(set2)
print(sym_diff_set)  # Output: {1, 2, 4, 5}
# Using the ^ operator
sym_diff_set = set1 ^ set2
print(sym_diff_set)  # Output: {1, 2, 4, 5}

 

 

Other Useful Set Methods

  • issubset(): Checks if the set is a subset of another set.
  • issuperset(): Checks if the set is a superset of another set.
  • isdisjoint(): Checks if two sets have no elements in common.
  • clear(): Removes all elements from the set.
  • copy(): Returns a shallow copy of the set.

Examples:

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
# Checking subset
print(set1.issubset(set2))  # Output: True
# Checking superset
print(set2.issuperset(set1))  # Output: True
# Checking disjoint sets
set3 = {6, 7, 8}
print(set1.isdisjoint(set3))  # Output: True
# Clearing a set
set1.clear()
print(set1)  # Output: set()
# Copying a set
set_copy = set2.copy()
print(set_copy)  # Output: {1, 2, 3, 4, 5}
 

 


PYTHON