About 5352 letters

About 27 minutes

#Python's set

A set is a fundamental mathematical model representing a collection of distinct objects. It is similar to a dictionary with only keys — in simple terms, elements in a set are unique.

Set literals are defined using curly braces ({}) with a group of values, for example:

fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'}
HashTable fruits fruits bucket1 Apple fruits->bucket1 Apple bucket2 Orange fruits->bucket2 Orange bucket3 Strawberry fruits->bucket3 Strawberry bucket4 Banana fruits->bucket4 Banana bucket5 Pineapple fruits->bucket5 Pineapple

The type annotation set[str] means a set where the element type is str.

#Empty Set

To create an empty set, use set() instead of {}, as the latter creates an empty dictionary.

#Creating Sets from a List or Tuple

You can create a set from a list or tuple. Duplicate values in the source will be filtered out, effectively deduplicating the data:

numbers_list:list[int] = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6] numbers_set:set[int] = set(numbers_list) print(numbers_list) print(numbers_set) numbers_list = list(numbers_set) print(numbers_list)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Checking Element Existence

Just like dictionaries, you can use in to check if an element exists in a set:

fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'} print('Strawberry' in fruits) print('Strawberry' not in fruits)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Adding Elements

Use the add method to add elements:

fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'} fruits.add('Grape') print(fruits)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Removing Elements

Use remove or discard to delete elements:

  • remove – deletes the specified element, raises an error if not found.
  • discard – deletes the specified element, does nothing if not found.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'} fruits.remove('Apple') # Remove 'Apple' fruits.discard('Banana') # Remove 'Banana' print(fruits)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Clearing a Set

Use the clear method to remove all elements:

fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Banana', 'Pineapple'} fruits.clear() print(fruits)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Merging Sets

Use the update method to merge another set:

fruits:set[str] = {'Apple', 'Orange', 'Strawberry'} fruits.update({'Banana', 'Pineapple'}) print(fruits)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Intersection

Use the intersection method or & operator to get the intersection of sets (elements common to both):

  • Both methods return a new set without modifying the original.
  • Use intersection_update to modify the original set in place.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Tomato', 'Cucumber'} vegetables:set[str] = {'Cabbage', 'Tomato', 'Cucumber', 'Spinach'} print(fruits.intersection(vegetables)) print(fruits & vegetables)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Union

Use the union method or | operator to get the union of sets (all unique elements from both):

  • Both methods return a new set without modifying the original.
  • Use update to modify the original set in place.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Tomato', 'Cucumber'} vegetables:set[str] = {'Cabbage', 'Tomato', 'Cucumber', 'Spinach'} print(fruits.union(vegetables)) print(fruits | vegetables)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Difference

Use the difference method or - operator to get the difference (elements only in the first set):

  • Both methods return a new set without modifying the original.
  • Use difference_update to modify the original set in place.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Tomato', 'Cucumber'} vegetables:set[str] = {'Cabbage', 'Tomato', 'Cucumber', 'Spinach'} print(fruits.difference(vegetables)) print(fruits - vegetables)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Symmetric Difference

Use the symmetric_difference method to get the symmetric difference (elements unique to each set):

  • Both methods return a new set without modifying the original.
  • Use symmetric_difference_update to modify the original set in place.
fruits:set[str] = {'Apple', 'Orange', 'Strawberry', 'Tomato', 'Cucumber'} vegetables:set[str] = {'Cabbage', 'Tomato', 'Cucumber', 'Spinach'} print(fruits.symmetric_difference(vegetables))

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Subsets and Supersets

If all elements of set A are in set B, then A is a subset of B, and B is a superset of A. If A and B are not equal, then A is a proper subset of B, and B is a proper superset of A.

Python uses comparison operators to check set relationships:

RelationDescription
A <= BA is a subset of B
A < BA is a proper subset of B
A >= BA is a superset of B
A > BA is a proper superset of B

Created in 5/15/2025

Updated in 5/21/2025