Sitemap
Data Engineering

Data Engineering

Data Engineer

Member-only story

2)Set methods in Python

2 min readSep 12, 2024

--

issubset()

Returns True if all elements of the set are present in the specified set, otherwise returns False.

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4}
result = set1.issubset(set2)
print(result)
# Output: True

issuperset()

Returns True if all elements of the specified set are present in the original set, otherwise returns False.

set1 = {1, 2, 3, 4}
set2 = {1, 2}
result = set1.issuperset(set2)
print(result)
# Output: True

isdisjoint()

Returns True if the two sets have no common elements, otherwise returns False.

set1 = {1, 2, 3}
set2 = {4, 5, 6}
result = set1.isdisjoint(set2)
print(result)
# Output: True

copy()

Returns a shallow copy of the set.

set1 = {1, 2, 3}
set_copy = set1.copy()
print(set_copy)
# Output: {1, 2, 3}

update() (|=)

Adds elements from another set or iterable to the set. This modifies the original set.

set1 = {1, 2, 3}
set1.update({3, 4, 5})
print(set1) # Output: {1, 2, 3, 4, 5}

set1 = {1, 2, 3}
set1 |= {3, 4, 5}
print(set1) # Output: {1, 2, 3, 4, 5}

intersection_update() (&=)

Updates the set by keeping only elements found in both sets.

set1 = {1, 2, 3}
set1.intersection_update({2, 3, 4})…

--

--