Python Sets

  • Read
  • Discuss

Introduction

Python sets are a built-in data type that can store an unordered collection of unique items. Sets are similar to lists and tuples but have some key differences. One of the main differences is that you can’t store duplicate values in sets. Sets are also not ordered, meaning that items in a set have no index or position. This can make them useful in certain situations where the order of items is not essential.

Following are properties of Python sets:

  • Unordered: Set does not maintain the order of data insertion
  • Immutable: We can not modify set items
  • Heterogeneous: sets can contain different types of data
  • Unique: set does not allow duplicate items

Types of Data That Can Be Stored in Python Sets

In Python, a set can store any type of data that is immutable, which means the value cannot be changed after it’s created. The common types of data stored in a Python set include

Let’s look at the examples

Creating a set of integers:

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

Creating a set of floating-point numbers

my_set = {3.14, 2.718, 1.618}

Creating a set of strings

my_set = {"hello", "world"}

Creating a set of Boolean values

my_set = {True, False}

Creating a set of other sets

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

Creating a set of Tuples

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

Sets built-in functions table

Here’s a table of some of the most commonly used built-in functions for manipulating sets in Python:

FunctionDescription
s.add(element)Adds an element to the set s
s.clear()Removes all elements from the set s
s.copy()Returns a shallow copy of the set s
s.difference(t)Returns a new set containing elements that are only in s and not in t
s.difference_update(t)Removes elements that are also in t from the set s
s.discard(element)Removes an element from the set s, but does not raise an error if the element is not in the set
s.intersection(t)Returns a new set containing elements that are common to both s and t
s.intersection_update(t)Removes elements from s that are not in t
s.isdisjoint(t)Returns True if s and t have no elements in common
s.issubset(t)Returns True if s is a subset of t
s.issuperset(t)Returns True if s is a superset of t
s.pop()Removes and returns an arbitrary element from the set s
s.remove(element)Removes an element from the set s and raises a KeyError if the element is not in the set
s.symmetric_difference(t)Returns a new set containing elements that are in s or t but not in both
s.symmetric_difference_update(t)Inserts elements into s that are not in t and removes elements from s that are in t
s.union(t)Returns a new set containing elements that are in either s or t or both
s.update(t)Inserts all elements of t into s

Implement Python Sets

Python sets store a collection of unique items. They are defined using curly braces {} or the built-in set() function, and items within a set are separated by commas. Here’s an example of how to create a set in Python:

# Creating a set using curly braces
my_set = {1, 2, 3, 4}
print(my_set)

# Creating a set using the set() function
my_set = set([1, 2, 3, 4])
print(my_set)

You can also create an empty set using the set() function, like this:

my_set = set()

You can add an item to a set using the add() method, and remove an item using the remove() method. Here’s an example:

# Adding an item to a set
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)

# Removing an item from a set
my_set.remove(3)
print(my_set)

You can also perform mathematical set operations such as union, intersection, and difference on sets. The union of two sets is a set that contains all the items from both sets. The union() method or the | operator can find the union of two sets. Here’s an example:

# Union of two sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1.union(set2)
print(set3)

You can also use the | operator for union

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

The intersection of two sets is a set that contains items that are common to both sets. The intersection() method or the & operator can be used to find the intersection of two sets. Here’s an example:

# Intersection of two sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = set1.intersection(set2)
print(set3)

You can also use & operator for intersection

set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = set1 & set2
print(set3)

The difference between two sets is a set that contains items that are in the first set but not in the second set. The difference() method or the – operator can be used to find the difference between two sets. Here’s an example:

# Difference of two sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = set1.difference(set2)
print(set3)

You can also use the – operator for difference:

set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = set1 - set2
print(set3)

Use Cases of Python Sets

Python sets are unordered collections of unique elements. They are used to store and manipulate collections of items where each item is unique and the order of items does not matter. Here are some common use cases for sets in Python:

  1. Removing duplicates from a list: If you have a list of items and you want to remove any duplicates, you can convert the list to a set, and then back to a list. This will remove any duplicates and preserve the order of the remaining items.
  2. Membership testing: You can use the “in” keyword to check if an item is present in a set. This is much faster than checking for membership in a list because sets are optimized for this operation.
  3. Set operations: Python sets support a variety of set operations, such as union, intersection, difference, and symmetric difference. These can be used to perform operations such as finding common items between sets, or items unique to each set.
  4. Creating a dictionary with distinct keys: Since sets only contain distinct items, it is useful when creating a dictionary where keys are not repeating.
  5. Iteration: Sets are just like lists in iteration. It can be useful when you want to process all the elements in a set one by one.
  6. Performance comparison: For large collections of items, a set is faster than a list or tuple when it comes to checking for membership or removing duplicates. This is because sets are implemented as hashtables, which have a constant time complexity for these operations.

Limitations of Python Sets

Python sets are powerful data structures that can be used in many different ways, but they also have some limitations that you should be aware of:

  1. Sets are unordered: Sets do not maintain any particular order for the elements they contain. This means that elements are not stored in any specific order, and you cannot use indexing or slicing to access specific elements.
  2. Sets only store unique elements: Sets only store distinct elements, so if you try to add an element to an already present set, it will not be added again. This can make it difficult to keep track of the number of times an element appears in a set.
  3. Sets are not mutable: Sets are not mutable, meaning you cannot change an element of a set after it has been added. This means you cannot change or modify a specific value in a set. You need to remove the value from the set and add it again with the new value.
  4. Sets only support basic data types: Sets only support immutable data types like int, float, strings, and tuples. They do not support more complex data types like lists or other sets.
  5. Sets do not support indexing: Sets do not support indexing, you can’t access specific elements using indexes like lists or tuples.

While these limitations may make sets less suitable for certain types of problems, they can be a useful tool for solving many other problems, especially when handling unique items and set operations.

Leave a Reply

Leave a Reply

Scroll to Top