Python Data Types – Sets

Python Set Data Type

In Python, a Set is an unordered data type that represents a collection of unique values. Sets are defined using curly braces {}, and each element is unique. For example:

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

Characteristics of Sets

1. No Duplicates Allowed

Since sets do not allow duplicate values, if the same value is added multiple times, only one instance is stored.

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

2. Unordered

Since sets are an unordered type, they do not support indexing or slicing. To access elements of a set, you must use a loop.

my_set = {"apple", "banana", "cherry"}
for item in my_set:
    print(item)

3. Adding and Removing Elements in a Set

Sets are mutable, which means you can add or remove elements. You can use the add() method to add elements, and remove() or discard() methods to remove them.

my_set = {1, 2, 3}
my_set.add(4)            # {1, 2, 3, 4}
my_set.remove(2)         # {1, 3, 4}
my_set.discard(5)        # {1, 3, 4} (no error when removing a non-existent element)
print(my_set)

4. Set Operations

Sets support various operations such as union, intersection, and difference. These operations can be performed using the |, &, and - operators or methods.

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

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

# Intersection
intersection_set = set1 & set2
print(intersection_set)  # {3, 4}

# Difference
difference_set = set1 - set2
print(difference_set)  # {1, 2}

5. Set Methods

Sets offer various methods for easy manipulation of elements:

  • set.add(x): Adds an element to the set.
  • set.remove(x): Removes a specific element from the set, and raises an error if the element is not present.
  • set.discard(x): Removes a specific element from the set, and does not raise an error if the element is not present.
  • set.union(other_set): Returns the union of two sets.
  • set.intersection(other_set): Returns the intersection of two sets.
  • set.difference(other_set): Returns the difference of two sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}

set1.add(6)
print(set1)  # {1, 2, 3, 6}

set1.discard(2)
print(set1)  # {1, 3, 6}

union = set1.union(set2)
print(union)  # {1, 3, 4, 5, 6}

intersection = set1.intersection(set2)
print(intersection)  # {3}

6. Applications of Sets

Sets are useful in various situations such as removing duplicate values or finding common elements through operations between multiple sets. For example, to remove duplicates from a list, you can convert it to a set.

my_list = [1, 2, 2, 3, 4, 4, 5]
my_set = set(my_list)
print(my_set)  # {1, 2, 3, 4, 5}

Summary

  • Sets represent a collection of unique values and do not allow duplicate values.
  • Sets are unordered, so they do not support indexing or slicing.
  • You can manipulate set elements using methods such as add(), remove(), and discard().
  • Sets support operations such as union, intersection, and difference, which allow for easy analysis of data relationships.
  • Sets can be used to remove duplicate values or find common elements, among other tasks.

Sets are one of the most useful data types in Python, particularly suited for handling duplicate data or performing set operations. Utilize the various features of sets to manage data efficiently!