Useful Set Methods in Python

Useful Set Methods in Python

add(elem)

Adds a unique element to the set. If the element already exists, the set remains unchanged.

Example: 

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4}

discard(elem)

Removes a specific element from the set. If the element does not exist, no changes are made and no exception is raised.

Example: 

my_set = {1, 2, 3}
my_set.discard(2)
print(my_set)  # Output: {1, 3}

remove(elem)

Removes a specific element from the set. If the element does not exist, a KeyError exception is raised.

Example: 

my_set = {1, 2, 3}
my_set.remove(2)
print(my_set)  # Output: {1, 3}

pop()

Removes and returns an arbitrary element from the set. Since sets are unordered, the removed element is unpredictable. If the set is empty, a KeyError is raised.

Example: 

my_set = {1, 2, 3}
element = my_set.pop()
print("Removed element:", element)
print(my_set)  # Output: set without the removed element

clear()

Removes all elements from the set, making it empty.

Example: 

my_set = {1, 2, 3}
my_set.clear()
print(my_set)  # Output: set()

copy()

Returns a shallow copy of the set. Changes made to the copy will not affect the original set.

Example: 

my_set = {1, 2, 3}
copy_set = my_set.copy()
copy_set.add(4)
print("Original set:", my_set)  # Output: {1, 2, 3}
print("Copied set:", copy_set)  # Output: {1, 2, 3, 4}

union(*sets)

Returns a new set containing all unique elements from the given sets. You can also use the | operator for the same operation.

Example: 

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

intersection(*sets)

Returns a new set containing elements that are common to all the given sets. You can also use the & operator for the same operation.

Example: 

set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {2, 3}

difference(*sets)

Returns a new set containing elements that are in the first set but not in the other given sets. You can also use the – operator for the same operation.

Example: 

set1 = {1, 2, 3}
set2 = {2, 3, 4}
difference_set = set1.difference(set2)
print(difference_set)  # Output: {1}

 symmetric_difference(set)

Returns a new set containing elements that are in either of the sets but not in both. You can also use the ^ operator for the same operation.

Example: 

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

 issubset(set)

Returns True if all elements of the set are present in the given set. Otherwise, returns False.

Example: 

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

 issuperset(set)

Returns True if all elements of the given set are present in the set. Otherwise, returns False.

Example: 

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

isdisjoint(set)

Certainly! Here’s a detailed guide on useful methods for working with sets in Python. Sets are unordered collections of unique elements. They provide various built-in methods to manipulate and query the data they contain.

Useful Set Methods in Python

add(elem)

Adds a unique element to the set. If the element already exists, the set remains unchanged.

Example: 

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4}

discard(elem)

Removes a specific element from the set. If the element does not exist, no changes are made and no exception is raised.

Example: 

my_set = {1, 2, 3}
my_set.discard(2)
print(my_set)  # Output: {1, 3}

remove(elem)

Removes a specific element from the set. If the element does not exist, a KeyError exception is raised.

Example: 

my_set = {1, 2, 3}
my_set.remove(2)
print(my_set)  # Output: {1, 3}

pop()

Removes and returns an arbitrary element from the set. Since sets are unordered, the removed element is unpredictable. If the set is empty, a KeyError is raised.

Example: 

my_set = {1, 2, 3}
element = my_set.pop()
print("Removed element:", element)
print(my_set)  # Output: Set without the removed element

clear()

Removes all elements from the set, leaving it empty.

Example: 

my_set = {1, 2, 3}
my_set.clear()
print(my_set)  # Output: set()

copy()

Returns a shallow copy of the set. Changes made to the copy do not affect the original set.

Example: 

my_set = {1, 2, 3}
copy_set = my_set.copy()
copy_set.add(4)
print("Original set:", my_set)  # Output: {1, 2, 3}
print("Copied set:", copy_set)  # Output: {1, 2, 3, 4}

union(*sets)

Returns a new set containing all unique elements from the given sets. You can also use the | operator for the same operation.

Example: 

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

intersection(*sets)

Returns a new set containing elements common to all the given sets. You can also use the & operator for the same operation.

Example: 

set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {2, 3}

difference(*sets)

Returns a new set containing elements present in the first set but not in the given sets. You can also use the – operator for the same operation.

Example: 

set1 = {1, 2, 3}
set2 = {2, 3, 4}
difference_set = set1.difference(set2)
print(difference_set)  # Output: {1}

 symmetric_difference(set)

Returns a new set containing elements that are in either of the sets but not in both. You can also use the ^ operator for the same operation.

Example: 

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

 issubset(set)

Returns True if all elements of the current set are present in the given set. Otherwise, returns False.

Example: 

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

issuperset(set)

Returns True if all elements of the given set are present in the current set. Otherwise, returns False.

Example: 

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

isdisjoint(set)

Returns True if the sets have no elements in common. Otherwise, returns False.

Example: 

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

Summary of Useful Methods

  • add(elem): Adds a unique element.
  • discard(elem): Removes an element without raising an exception if the element does not exist.
  • remove(elem): Removes an element and raises an exception if the element does not exist.
  • pop(): Removes and returns an arbitrary element.
  • clear(): Removes all elements.
  • copy(): Creates a shallow copy.
  • union(*sets): Union of multiple sets.
  • intersection(*sets): Intersection of multiple sets.
  • difference(*sets): Difference between sets.
  • symmetric_difference(set): Symmetric difference between sets.
  • issubset(set): Checks if all elements are in another set.
  • issuperset(set): Checks if the set contains all elements of another set.
  • isdisjoint(set): Checks if sets have no elements in common.

These methods allow you to efficiently manipulate sets in Python, performing various operations for comparison, modification, and querying.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print