Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Sets in Python Programming

A comprehensive guide to sets, a fundamental data type in Python programming. …


Updated July 30, 2023

A comprehensive guide to sets, a fundamental data type in Python programming.

Understanding Sets in Python

Definition of Sets


In Python, a set is an unordered collection of unique elements. It’s essentially a list without duplicates, making it a powerful tool for working with large datasets or performing complex operations on unique values.

Key Characteristics of Sets:

  • Unordered: The order in which elements are stored in a set is irrelevant.
  • Unique: Each element in a set must be distinct; duplicates are not allowed.
  • Mutable: Sets can be modified after they’re created.

Using Sets with Variables and Data Types

In Python, you can create sets from various data types, including lists, tuples, dictionaries, and other sets. Let’s explore some examples:

Creating a Set from a List

You can use the set() function to convert a list into a set:

# Define a list with duplicates
my_list = [1, 2, 3, 4, 5, 2, 6]

# Create a set from the list
my_set = set(my_list)

print(my_set)  # Output: {1, 2, 3, 4, 5, 6}

Creating a Set from a Tuple

Tuples are similar to lists but immutable. You can also create a set from a tuple using the set() function:

# Define a tuple with duplicates
my_tuple = (1, 2, 3, 4, 5, 2)

# Create a set from the tuple
my_set = set(my_tuple)

print(my_set)  # Output: {1, 2, 3, 4, 5}

Creating a Set from Another Set

If you already have a set and want to create another set with some modifications, you can use the set() function:

# Define an initial set
initial_set = {1, 2, 3}

# Create a new set by adding elements to the initial set
new_set = set(initial_set) | {4, 5}  # Use union operation (|)

print(new_set)  # Output: {1, 2, 3, 4, 5}

Common Set Operations in Python

Python provides several built-in methods for performing common set operations:

  • Union (|): Returns a new set containing all elements from both sets.
  • Intersection (&): Returns a new set containing only the elements that are present in both sets.
  • Difference (-): Returns a new set containing all elements that are present in one set but not the other.
  • Symmetric Difference (^): Returns a new set containing all elements that are present in either set but not both.

Here are some examples:

# Define two sets
set1 = {1, 2, 3}
set2 = {4, 5, 6}

# Union operation
union_set = set1 | set2

print(union_set)  # Output: {1, 2, 3, 4, 5, 6}

# Intersection operation
intersection_set = set1 & set2

print(intersection_set)  # Output: set()

# Difference operation
difference_set = set1 - set2

print(difference_set)  # Output: {1, 2, 3}

Conclusion


In this comprehensive guide to sets in Python, we’ve explored the definition of sets, their characteristics, and various operations that can be performed on them. With practice and experience, you’ll become proficient in using sets to efficiently manipulate data in your programs.

By mastering the basics of sets and other fundamental data types, you’ll gain a solid foundation for tackling more complex topics in Python programming.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp