Python Sets

In Python, sets are a fundamental data type used to store a collection of unique items. They are similar to lists and tuples, but unlike those data types, sets do not maintain any order and each item can only appear once. Sets are written using curly braces and separated by commas. For example:

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

Sets are often used to perform set operations such as union, intersection, and difference. They can also be used to check if an element is in a set or to remove duplicates from a list.

Python provides several built-in functions and methods for working with sets, such as add() which adds an element to the set, remove() which removes an element from the set, and intersection() which returns a new set containing items that exist in both sets.

For example,

1numbers = {1, 2, 3, 4, 5} 2numbers.add(6) # add an element to the set 3numbers.remove(2) # remove an element from the set 4even_numbers = {2, 4, 6, 8} 5print(numbers.intersection(even_numbers)) # Output: {4, 6}

In addition, you can use set comprehension to create a new set by applying an operation to each element of an existing set.

1numbers = {1, 2, 3, 4, 5} 2squared_numbers = {x**2 for x in numbers} 3print(squared_numbers) # Output: {1, 4, 9, 16, 25}

Python sets are a fundamental data type used to store a collection of unique items. They are often used to perform set operations such as union, intersection, and difference, and to check if an element is in a set or to remove duplicates from a list. Python provides several built-in functions and methods for working with sets, such as add(), remove(), and intersection(), that allow you to perform a wide range of operations on sets. In addition, you can use set comprehension to create a new set by applying an operation to each element of an existing set. Understanding how to use sets and the different functions and methods available in Python is an essential part of becoming proficient in the language.

Set Operations

In addition to adding and removing elements, sets support several operations for working with multiple sets. Here are a few common set operations:

  • Union: returns a new set containing all the unique elements from both sets
  • Intersection: returns a new set containing only the elements that are in both sets
  • Difference: returns a new set containing only the elements that are in the first set but not the second set
  • Symmetric difference: returns a new set containing only the elements that are in either set, but not in both

Here are examples of each set operation:

1set_a = {1, 2, 3, 4} 2set_b = {3, 4, 5, 6} 3 4# union 5union_set = set_a.union(set_b) 6print(union_set) # output: {1, 2, 3, 4, 5, 6} 7 8# intersection 9intersection_set = set_a.intersection(set_b) 10print(intersection_set) # output: {3, 4} 11 12# difference 13difference_set = set_a.difference(set_b) 14print(difference_set) # output: {1, 2} 15 16# symmetric difference 17symmetric_difference_set = set_a.symmetric_difference(set_b) 18print(symmetric_difference_set