An Introduction to Python Sets

Introduction

In Python, a set is an unordered collection of unique elements. This means that each element in a set must be unique, and the order of the elements does not matter. Sets are mutable, meaning that they can be changed after they are created.

Creating Sets

There are two ways to create a set in Python:

1) Using the set() function

2) Using a set literal

The set() function takes an iterable as its argument and returns a set containing the elements of the iterable. For example, the following code creates a set containing the numbers 1, 2, and 3:

Code snippet

>>> set([1, 2, 3])

{1, 2, 3}


A set literal is a collection of elements enclosed in curly braces. The elements of a set literal do not need to be enclosed in quotation marks. For example, the following code creates a set containing the strings "hello" and "world":

Code snippet

>>> {"hello", "world"}

{"hello", "world"}


Adding and Removing Elements

Elements can be added to a set using the add() method. For example, the following code adds the number 4 to the set created in the previous example:

Code snippet

>>> my_set.add(4)

>>> my_set

{1, 2, 3, 4}


Elements can be removed from a set using the remove() method. For example, the following code removes the number 4 from the set created in the previous example:

Code snippet

>>> my_set.remove(4)

>>> my_set

{1, 2, 3}


Iteration

Sets can be iterated over using a for loop. For example, the following code prints the elements of the set created in the first example:

Code snippet

>>> for element in my_set:

...     print(element)

... 

1

2

3


Set Operations

Sets support a number of set operations, including union, intersection, difference, and symmetric difference. The union of two sets is the set that contains all of the elements of both sets. The intersection of two sets is the set that contains only the elements that are common to both sets. The difference of two sets is the set that contains the elements that are in the first set but not in the second set. The symmetric difference of two sets is the set that contains the elements that are in either the first set or the second set, but not both.

For example, the following code shows how to find the union, intersection, difference, and symmetric difference of two sets:

Code snippet

>>> set1 = {1, 2, 3}

>>> set2 = {2, 4, 5}

>>> 

>>> set1.union(set2)

{1, 2, 3, 4, 5}

>>> set1.intersection(set2)

{2}

>>> set1.difference(set2)

{1, 3}

>>> set1.symmetric_difference(set2)

{1, 3, 4, 5}


Frozen Sets

A frozen set is an immutable version of a set. This means that the elements of a frozen set cannot be added to, removed from, or changed in any way. Frozen sets are created using the frozenset() function. For example, the following code creates a frozen set from the set created in the first example:

Code snippet

>>> my_frozen_set = frozenset(my_set)

>>> my_frozen_set

frozenset({1, 2, 3})


Conclusion

Sets are a powerful data structure that can be used for a variety of tasks. They are unordered, mutable, and support a number of set operations. Frozen sets are a special type of set that is immutable.

I hope this blog has helped you learn about Python sets.

Happy Coding!



Next Post Previous Post
1 Comments
  • Harshahv
    Harshahv 17 June 2023 at 15:23

    Publish More

Add Comment
comment url