Skip to main content
Geosciences LibreTexts

8.1: Introduction to sets

  • Page ID
    26494
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    We already learned about lists, which is one of the most important data structures.  A related data structure is the set.  Unlike lists, sets ensure that there are no duplicate entries, making them useful for tasks that require uniqueness among items.

    A practical example of when you'd use a set in a program is when you need to remove duplicates from a list of items. Imagine you're developing an application that collects user responses from a survey where each response includes interests or hobbies. Since users might enter duplicate interests, your task is to summarize the unique interests across all responses without repetition.  You can use sets for this.

    Sets in Python also support a rich set of operations, mirroring the concepts found in mathematical set theory. These include union, intersection, difference, and symmetric difference. Such operations are not just syntactically straightforward but are also optimized for performance, further underscoring the utility of sets in scenarios involving large datasets. For example, finding common elements between two sets (intersection) or combining elements from multiple sets while maintaining uniqueness (union) can be done efficiently with set operations.

    Here are examples demonstrating different methods of constructing sets in Python:

    • Direct Method: When you use curly braces `{}`, you can directly define a set with unique elements. For example, direct_set = {1, 2, 3, 4, 5} creates a set with elements 1 through 5. The output for this is {1, 2, 3, 4, 5}.
    • Using `set()` Constructor with a List: You can also create a set from a list by passing the list as an argument to the `set()` constructor. This method automatically removes any duplicate elements. For instance, if you have a list [1, 2, 2, 3, 4], you can convert this to a set using this command:

    list_to_set = set([1, 2, 2, 3, 4])

    which converts it into a set {1, 2, 3, 4}.  Remember that sets do not allow duplicate items, so when you convert this list into a set, one of the 2s disappear.

    If you want to create an empty set, you must use the `set()` constructor without any arguments, as `{}` will create an empty dictionary instead. An example is empty_set = set(), which results in set(), representing an empty set.

    The `remove` and `add` methods in Python's set data structure are used for manipulating set contents. The `remove` method is used to delete a specific element from a set.  If the specified element is present in the set, it is removed; however, if the element does not exist in the set, Python raises a `KeyError`. This underscores the importance of either checking for an element's existence before attempting to remove it or using the `discard` method instead, which does not raise an error if the element is not found. The `remove` method is particularly useful when you need to ensure that an operation only proceeds if the element to be deleted is indeed a part of the set, providing a strict control mechanism over set manipulation.

    The `add` method is used to insert a new element into a set. The uniqueness of sets ensures that if the element already exists within the set, adding it again has no effect, maintaining the set's invariant of containing unique elements only. This method is straightforward and does not require checking for the element's presence beforehand, making it an efficient way to ensure that a set contains a given element.

    Here are examples demonstrating the use of the `remove` and `add` methods with a Python set:

    We start with a set `{1, 2, 3, 4, 5}`. It's a collection of unique integers from 1 to 5.

    Using the `remove` Method: When we apply my_set.remove(3), the element 3 is removed from the set, resulting in {1, 2, 4, 5}.

    Using the `add` Method: Adding a new element to the set with my_set.add(6) updates the set to {1, 2, 4, 5, 6}.

    Adding an Existing Element: Trying to add an element that's already in the set, such as 2, does not change the set because sets cannot have duplicate items in it.


    8.1: Introduction to sets is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?