📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
  • Creating a Set in Python
  • Adding and Removing Elements from a Set
  • Basic Set Operations
  • Iterating through a set
  • Set Comprehension
  • Similarities:
  • Differences:
  • An Example
Edit on GitHub
  1. 10 - Sets and dictionaries

Sets

Previous10 - Sets and dictionariesNextDictionaries

Last updated 1 year ago

In Python, a set is an unordered collection of unique elements. It is similar to a list or a tuple but with a few differences. One of the most important differences is that a set only contains unique elements, meaning that no duplicates are allowed. Another important difference is that sets are mutable, which means that you can add or remove elements from them. Sets are very useful when it comes to solving problems that involve finding unique elements, such as removing duplicates or determining the intersection of two lists.

Creating a Set in Python

To create a set in Python, you can use either curly braces or the built-in set() function. For example:

# Using curly braces
my_set = {1, 2, 3, 4, 5}

# Using the set() function
my_set = set([1, 2, 3, 4, 5])

In both cases, we create a set that contains the numbers 1 through 5.

Adding and Removing Elements from a Set

One of the main benefits of using a set is the ability to easily add and remove elements from it. To add an element to a set, you can use the add() method. For example:

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

To remove an element from a set, you can use the remove() method. For example:

>>> my_set = {1, 2, 3}
>>> my_set.remove(3)
>>> print(my_set) 
 {1, 2}
>>>

You can also use the discard() method to remove an element from a set, but it won't raise an error if the element is not found in the set. For example:

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

Basic Set Operations

In addition to adding and removing elements from a set, there are several other useful set operations. Here are a few examples:

  • Union: The union of two sets is a new set that contains all the elements from both sets, with no duplicates.

>>> set1 = {1, 2, 3}
>>> set2 = {3, 4, 5}
>>> union_set = set1.union(set2)
>>> print(union_set) 
 {1, 2, 3, 4, 5}
>>>
  • Intersection: The intersection of two sets is a new set that contains only the elements that are common to both sets.

>>> set1 = {1, 2, 3}
>>> set2 = {3, 4, 5}
>>> intersection_set = set1.intersection(set2)
>>> print(intersection_set) 
 {3}
>>> 
  • Difference: The difference of two sets is a new set that contains only the elements that are in one set but not the other.

>>> set1 = {1, 2, 3}
>>> set2 = {3, 4, 5}
>>> difference_set = set1.difference(set2)
>>> print(difference_set) 
 {1, 2}
>>> 
  • Symmetric Difference: The symmetric difference of two sets is a new set that contains only the elements that are in one set or the other, but not both.

>>>  set1 = {1, 2, 3}
>>> set2 = {3, 4, 5}
>>> symmetric_difference_set = set1.symmetric_difference(set2)
>>> print(symmetric_difference_set) 
 {1, 2, 4, 5} 
>>> 

Sets can also be used to remove duplicates from a list. Here is an example:

>>> my_list = [1, 2, 3, 2, 1, 4, 5, 4]
>>> my_set = set(my_list)
>>> my_list = list(my_set)
>>> print(my_list)
 [1, 2, 3, 4, 5]
>>> 

Iterating through a set

You can iterate through a set using a for loop in Python. Here's an example:

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

for item in my_set:
    print(item)

In this example, the for loop iterates through each item in the my_set set, and prints each item to the console. The order in which the items are printed may not be in the same order as they were added to the set, as sets are unordered data structures.

Set Comprehension

Similarities:

  1. Comprehension Syntax: Both list and set comprehensions use a similar comprehension syntax, where you define an expression to generate new values based on items from an iterable.

    • List Comprehension: [expression for item in iterable if condition]

    • Set Comprehension: {expression for item in iterable if condition}

  2. Iteration: Both comprehensions iterate over an iterable (e.g., a list, range, or another iterable) and apply an expression to each item to generate new values.

Differences:

  1. Uniqueness:

    • List Comprehension: Retains all values from the iterable, including duplicates.

    • Set Comprehension: Automatically removes duplicates, preserving only unique values.

  2. Order:

    • List Comprehension: Maintains the order of items from the original iterable.

    • Set Comprehension: Does not guarantee a specific order; the result may be unordered.

An Example

Here's a brief example illustrating the differences:

# List Comprehension
numbers = [1, 2, 2, 3, 4, 4, 5]
squared_list = [x**2 for x in numbers]
# squared_list is a list: [1, 4, 4, 9, 16, 16, 25]

# Set Comprehension
unique_squares = {x**2 for x in numbers}
# unique_squares is a set: {1, 4, 9, 16, 25}

In this example, list comprehension produces a list squared_list that retains duplicate values, while set comprehension generates a set unique_squares that automatically removes duplicates, resulting in a set of unique squared values.

Set comprehensions are particularly useful when you want to extract unique elements from an iterable, as they simplify the process of deduplication. However, if you need to maintain the order of items or keep duplicates, list comprehensions are more appropriate. Understanding the similarities and differences between these comprehensions helps you choose the right tool for your specific data manipulation needs.

Set comprehension in Python shares similarities with but has distinct characteristics that make it a valuable tool in certain situations.

list comprehension