Python Sets

A set is a collection of unique elements, which means that there are no duplicate values in a set. Sets are defined by curly braces { } or the set() function.

In Python, sets are implemented as unordered collections of unique elements, which means that the order of elements in a set is not preserved.

Creating Sets

There are several ways to create a set in Python.

The most common way is to use curly braces { } and separate the elements with commas.

For example:

# Creating an empty set
empty_set = set()

# Creating a set with elements
numbers = {1, 2, 3, 4, 5}

# Creating a set from a list
numbers_list = [1, 2, 3, 4, 5]
numbers_set = set(numbers_list)

# Creating a set from a string
string = "hello"
string_set = set(string)

print(empty_set)
print(numbers)
print(numbers_set)
print(string_set)

Output:

set()
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
{'l', 'o', 'h', 'e'}

Adding and Removing Elements

To add an element to a set, we can use the add() method. To remove an element from a set, we can use the remove() method. If the element to be removed does not exist in the set, a KeyError will be raised. To avoid this, we can use the discard() method, which removes an element if it exists in the set, but does nothing if it does not.

Here is an example:

# Creating a set
numbers = {1, 2, 3, 4, 5}

# Adding an element to a set
numbers.add(6)
print(numbers)

# Removing an element from a set
numbers.remove(6)
print(numbers)

# Removing an element safely
numbers.discard(6)
print(numbers)

Output:

{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}

Set Operations

Python provides several set operations such as union, intersection, difference, and symmetric difference.

The union() method returns a set that contains all elements from the original set and all elements from the set passed as an argument.

The intersection() method returns a set that contains only the elements that are common to both sets.

The difference() method returns a set that contains only the elements that are in the original set but not in the set passed as an argument.

The symmetric_difference() method returns a set that contains only the elements that are in one set and not in the other.

Here is an example:

# Creating sets
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

# Union
print(A.union(B))

# Intersection
print(A.intersection(B))

# Difference
print(A.difference(B))

# Symmetric difference
print(A.symmetric_diffetric_difference(B))

Output:

{1, 2, 3, 4, 5, 6, 7, 8}
{4, 5}
{1, 2, 3}
{1, 2, 3, 6, 7, 8}

Frozen Sets

A frozen set is a set that can’t be modified after it’s created. Frozen sets are defined by the frozenset() function or by using the set() function with a tuple as an argument.

Here is an example:

Creating a frozen set:

frozen_set = frozenset({1, 2, 3, 4, 5})

Attempting to add an element to a frozen set:

frozen_set.add(6)
except AttributeError as e:
print(e)

Output:

'frozenset' object has no attribute 'add'

Python Sets:

A set is a collection of unique elements, which means that there are no duplicate values in a set.

Sets are defined by curly braces { } or the set() function.

In Python, sets are implemented as unordered collections of unique elements, which means that the order of elements in a set is not preserved.

Access Set Items:

We can access the elements of a set using the square brackets [ ] or the for loop.

The elements of a set are not ordered, so we can’t use an index to access a specific element.

Here is an example:

# Creating a set
numbers = {1, 2, 3, 4, 5}

# Accessing an element using square brackets
print(numbers[0]) # This will raise a TypeError

# Accessing elements using a for loop
for number in numbers:
print(number)

Output:

TypeError: 'set' object does not support indexing
1
2
3
4
5

Add Set Items:

We can add elements to a set using the add() method. If the element already exists in the set, it will not be added again. Here is an example:

# Creating a set
numbers = {1, 2, 3, 4, 5}

# Adding an element to a set
numbers.add(6)
print(numbers)

Output:

{1, 2, 3, 4, 5, 6}

Remove Set Items:

We can remove elements from a set using the remove() method.

If the element to be removed does not exist in the set, a KeyError will be raised.

To avoid this, we can use the discard() method, which removes an element if it exists in the set, but does nothing if it does not.

# Creating a set
numbers = {1, 2, 3, 4, 5}

# Removing an element from a set
numbers.remove(6)
print(numbers)

# Removing an element safely
numbers.discard(6)
print(numbers)

Output:

KeyError: 6
{1, 2, 3, 4, 5}

Loop Sets:

We can loop through the elements of a set using the for loop.

The elements of a set are not ordered, so the order of the elements in the output may be different from the order of the elements in the set.

# Creating a set
numbers = {1, 2, 3, 4, 5}

# Looping through the elements of a set
for number in numbers:
print(number)

Output:

1
2
3
4
5

Join Sets:

We can join two sets using the union() method or the update() method.

The union() method returns a new set that contains all elements from both sets, while the update() method adds all elements from one set to the other.

# Creating sets
A = {1, 2, 3}
B = {3, 4, 5}

# Joining sets using union()
C = A.union(B)
print(C)

# Joining sets using update()
A.update(B)
print(A)

Output:

{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}

Set Methods:

Python provides a number of built-in methods for sets, which can be used to perform various set operations. Some of the most commonly used set methods are:

  • add() : This method adds an element to a set. If the element already exists in the set, it will not be added again.
  • clear() : This method removes all elements from a set.
  • copy() : This method returns a shallow copy of a set.
  • difference() : This method returns a set containing the difference between two sets. The difference between two sets is the set of elements that are present in the first set, but not in the second set.
  • discard() : This method removes an element from a set. If the element does not exist in the set, the method does nothing.
  • intersection() : This method returns a set containing the intersection of two sets. The intersection of two sets is the set of elements that are present in both sets.
  • isdisjoint() : This method returns True if two sets have no elements in common.
  • issubset() : This method returns True if a set is a subset of another set.
  • issuperset() : This method returns True if a set is a superset of another set.
  • pop() : This method removes and returns a random element from a set.
  • remove() : This method removes an element from a set. If the element does not exist in the set, a KeyError will be raised.
  • symmetric_difference() : This method returns a set containing the symmetric difference of two sets. The symmetric difference of two sets is the set of elements that are present in either set, but not in both sets.
  • union() : This method returns a set containing the union of two sets. The union of two sets is the set of elements that are present in either set or in both sets.
  • update() : This method adds all elements from one set to another set.

By understanding and using these set methods, we can perform various set operations and manipulate sets in an efficient way.

In conclusion, sets are a powerful data structure in Python that allows us to perform various set operations such as union, intersection, difference, and symmetric difference. We can also add, remove and loop through set items.

Additionally, Python provides a number of built-in methods for sets that can be used to perform various set operations in an efficient way.

Related Posts: