×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION DRF BASICS Roadmap
     ❯   

VARIABLES & TYPES IN PYTHON

Set type

Understanding Python Set Types: set and frozenset

Introduction

In Python, sets are a powerful data structure that allows you to store an unordered collection of unique elements. Python provides two types of sets: set and frozenset. While both are used to manage collections of unique elements, they differ in mutability, which dictates how and when they can be modified.

Set

A set in Python is an unordered collection of unique elements. This means that no element is repeated, and the elements are not stored in any particular order. Sets are mutable, allowing you to add or remove elements after the set is created.

Creating a Set

You can create a set by enclosing elements in curly braces {}. If duplicate elements are provided, they will be automatically removed.

Example:

my_set = {1, 2, 3, 4, 4}
print(my_set)

Output:

{1, 2, 3, 4}

In this example, the set my_set contains the elements 1, 2, 3, and 4. The duplicate 4 is automatically removed, leaving only unique elements in the set.

Set Operations

Sets support several operations that are useful for mathematical set theory, such as union, intersection, and difference.

Union:

The union of two sets contains all elements from both sets, with duplicates removed.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
print(union_set)

Output:

{1, 2, 3, 4, 5}

Intersection:

The intersection of two sets contains only the elements that are present in both sets.

intersection_set = set1 & set2
print(intersection_set)

Output:

{3}

Difference:

The difference between two sets contains elements that are in the first set but not in the second.

difference_set = set1 - set2
print(difference_set)

Output:

{1, 2}

Frozenset

A frozenset is an immutable version of a set. This means that once a frozenset is created, it cannot be modified; no elements can be added or removed. Frozensets are useful in situations where you need a set-like collection of elements that should not change, such as when using sets as dictionary keys or elements of other sets.

Creating a Frozenset

You can create a frozenset by passing an iterable (such as a list) to the frozenset() constructor.

Example:

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

Output:

frozenset({1, 2, 3})

In this example, my_frozenset contains the elements 1, 2, and 3. The frozenset is immutable, meaning no changes can be made to it after its creation.

Conclusion

Python's set and frozenset types offer flexible ways to manage collections of unique elements. While sets are mutable and allow for modifications, frozensets are immutable and are best suited for situations where a constant collection is needed. Both types support a variety of operations that make them powerful tools in Python programming.


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

© 2024 Nischal Lamichhane. All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.