Python From Beginner to Advanced

0% completed

Previous
Next
Python - Set Methods

Python sets come with a comprehensive set of methods that allow for powerful and efficient manipulation of set data. These methods facilitate operations ranging from adding and removing elements to comparing sets and performing mathematical set operations.

Set Methods

Here is a table of the most commonly used set methods along with rephrased descriptions for better clarity:

MethodDescriptionTime Complexity
add()Adds an element to the set, if it is not already present.O(1)
clear()Empties the set, removing all elements.O(n)
copy()Creates a copy of the set that is independent of the original.O(n)
difference()Returns a new set containing elements in the first set but not in others.O(n)
difference_update()Modifies the set by removing elements found in other sets.O(n)
discard()Removes a specified element from the set if it is present.O(1)
intersection()Returns a new set with elements common to the set and all others.O(n)
intersection_update()Updates the set by keeping only elements found in it and all others.O(n)
isdisjoint()Returns True if two sets have no elements in common.O(n)
issubset()Checks if the set is a subset of another set and returns True if so.O(n)
issuperset()Checks if the set is a superset of another set and returns True if so.O(n)
pop()Removes and returns an arbitrary element from the set; raises KeyError if empty.O(1)
remove()Removes a specified element from the set; raises KeyError if not present.O(1)
symmetric_difference()Returns a new set with elements in either the set or other but not in both.O(n)
symmetric_difference_update()Updates the set with elements in either the set or other but not in both.O(n)
union()Returns a new set with all elements from the set and all others.O(n)
update()Adds all elements from other sets to the set.O(n)

Example 1: Using Basic Set Methods

In this example, we will demonstrate the use of add, remove, and clear methods.

Python3
Python3

. . . .

Explanation:

  • sports.add('tennis') adds 'tennis' to the set, demonstrating how to include new elements.
  • sports.remove('soccer') removes 'soccer' from the set, showing element removal by value.
  • sports.clear() removes all elements, emptying the set entirely.

Example 2: Using Advanced Set Methods

In this example, we explore union, intersection, and difference methods.

Python3
Python3

. . . .

Explanation:

  • union_result = set_a.union(set_b) calculates the union, combining elements from both sets without duplication.
  • intersection_result = set_a.intersection(set_b) finds the intersection, identifying common elements between the two sets.
  • difference_result = set_a.difference(set_b) computes the difference, showing elements unique to set_a.

These examples illustrate how to utilize some of the fundamental set methods in Python to manipulate and compare sets effectively. These operations are crucial for data analysis and mathematical computations involving sets.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next