Set and set() in Python

What is a Python Set?

A Python Set is an unordered collection of unique elements. In Python programming, a set is used to store and manipulate items with the guarantee that each item is unique. Sets do not maintain any particular order of elements.

Creating Sets in Python

To initialize a set in Python, you can use the set() function. For example, create an empty set with my_set = set(), or initialize a set with elements by passing them as parameters: my_set = set([1, 2, 3]).

Adding and Removing Elements

To add an element to an existing set, use the add() method:

my_set.add(4)

To remove an element from a set, use the remove() method:

my_set.remove(2)

Set Operations

Sets support various operations such as union, intersection, and difference:

Union: Combine elements from two sets.

set1.union(set2)  
# or
set1 | set2

Intersection: Find common elements between two sets.

set1.intersection(set2)  
# or
set1 & set2

Difference: Find elements in one set but not in another.

set1.difference(set2)  
# or
set1 - set2

Definition of a Set in Python

In Python, a set is a built-in data structure used to store a collection of unique items. It is mutable, meaning its elements can be modified, added, or removed. Sets are unordered, so the items are not indexed and can appear in any order.

Key Characteristics

  • Unique Elements: Each item in a set is unique.
  • Unordered: Sets do not maintain order.
  • Immutable Elements: Sets can only contain immutable data types (e.g., integers, strings, tuples).

Creating Sets in Python

Using Curly Braces {}

You can create a set directly using curly braces:

my_set = {1, 2, 3}

Using the set() Constructor

Another way to create a set is by using the set() function:

my_list = [1, 2, 2, 3, 3, 4]
my_set = set(my_list) 

This removes duplicates and creates a set with the unique elements.

Unordered Collection

Sets are unordered collections of unique elements. This means the items in a set do not have a defined order and can appear in any order every time the set is used.

Characteristics of Unordered Collections

  • No Indexing: You cannot access elements by index.
  • Efficient Membership Testing: Quickly determine if an element is present.
  • Automatic Duplicate Removal: Adding an existing element to a set has no effect.

Immutable Types

Immutable types in Python cannot be changed once created. Examples include integers, floats, strings, and tuples. These types are hashable and can be used as elements in sets.

Overview of Sets Being Mutable/Immutable

Immutable Sets (Frozen Sets)

Immutable sets, created using the frozenset() constructor, cannot be modified after creation.

Mutable Sets

Mutable sets, created using the set() constructor, can be modified during program execution.

Operations on Mutable Sets

Common operations include:

  • Adding elements with add()
  • Removing elements with remove()
  • Updating the set with elements from another set using update()

Duplicate Elements

Sets do not allow duplicate elements. If you try to add an element that already exists, it will be ignored.

Handling Duplicates

Duplicates are automatically handled and removed when adding elements to a set. This makes sets efficient for maintaining unique collections of items.

By understanding these key aspects of Python sets, you can effectively use them to manage collections of unique elements in your programs.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate