Python Set Methods (original) (raw)

Python set methods are built-in functions used to add, remove, update and perform other operations on sets. These methods help manage and manipulate set elements efficiently.

add()

Adds an element to the set.

**Syntax: set_name.add(element)

In the code below, we will add an element to a set.

Python `

s = {1, 2, 3} s.add(4) print(s)

`

clear()

Removes all elements from the set.

**Syntax: set_name.clear()

In the code below, we will remove all elements from the set.

Python `

s = {1, 2, 3} s.clear() print(s)

`

copy()

Returns a shallow copy of the set.

**Syntax: set_name.copy()

In the code below, we will create a copy of a set.

Python `

s = {1, 2, 3} c = s.copy() print(c)

`

difference()

Returns a set containing elements present in the first set but not in the second set.

**Syntax: set1.difference(set2)

In the code below, we will find the difference between two sets.

Python `

a = {1, 2, 3, 4} b = {3, 4, 5} print(a.difference(b))

`

difference_update()

Removes common elements from the original set.

**Syntax: set1.difference_update(set2)

In the code below, we will update the set by removing common elements.

Python `

a = {1, 2, 3, 4} b = {3, 4, 5} a.difference_update(b) print(a)

`

discard()

Removes an element from the set if it exists.

**Syntax: set_name.discard(element)

In the code below, we will remove an element from the set.

Python `

s = {1, 2, 3} s.discard(2) print(s)

`

frozenset()

Creates an immutable set.

**Syntax: frozenset(iterable)

In the code below, we will create a frozen set.

Python `

s = frozenset([1, 2, 3]) print(s)

`

Output

frozenset({1, 2, 3})

intersection()

Returns common elements from two or more sets.

**Syntax: set1.intersection(set2)

In the code below, we will find common elements.

Python `

a = {1, 2, 3} b = {2, 3, 4} print(a.intersection(b))

`

intersection_update()

Updates the set with only the elements that are common to both sets.

**Syntax: set1.intersection_update(set2)

In the code below, we will update the set to keep only common elements.

Python `

a = {1, 2, 3} b = {2, 3, 4} a.intersection_update(b) print(a)

`

isdisjoint()

Returns True if two sets have no common elements.

**Syntax: set1.isdisjoint(set2)

In the code below, we will check whether two sets are disjoint.

Python `

a = {1, 2} b = {3, 4} print(a.isdisjoint(b))

`

issubset()

Returns True if all elements of one set are present in another set.

**Syntax: set1.issubset(set2)

In the code below, we will check whether one set is a subset of another.

Python `

a = {1, 2} b = {1, 2, 3, 4} print(a.issubset(b))

`

issuperset()

Returns True if a set contains all elements of another set.

**Syntax: set1.issuperset(set2)

In the code below, we will check whether a set is a superset of another set.

Python `

a = {1, 2, 3, 4} b = {1, 2} print(a.issuperset(b))

`

pop()

Removes and returns a random element from the set.

**Syntax: set_name.pop()

In the code below, we will remove a random element from the set.

Python `

s = {1, 2, 3} print(s.pop()) print(s)

`

**Note: The removed element may vary because sets are unordered.

remove()

Removes the specified element from the set.

**Syntax: set_name.remove(element)

In the code below, we will remove an element from the set.

Python `

s = {1, 2, 3} s.remove(2) print(s)

`

symmetric_difference()

Returns elements that are present in either set but not in both.

**Syntax: set1.symmetric_difference(set2)

In the code below, we will find the symmetric difference of two sets.

Python `

a = {1, 2, 3} b = {3, 4, 5} print(a.symmetric_difference(b))

`

symmetric_difference_update()

Updates the set with the symmetric difference of two sets.

**Syntax: set1.symmetric_difference_update(set2)

In the code below, we will update the set with elements that are not common.

Python `

a = {1, 2, 3} b = {3, 4, 5} a.symmetric_difference_update(b) print(a)

`

union()

Returns a new set containing all unique elements from the sets.

**Syntax: set1.union(set2)

In the code below, we will combine two sets.

Python `

a = {1, 2, 3} b = {3, 4, 5} print(a.union(b))

`

update()

Adds all elements from another iterable to the set.

**Syntax: set1.update(iterable)

In the code below, we will add elements from another set.

Python `

a = {1, 2} b = {3, 4} a.update(b) print(a)

`