AI Glossary / Data science and analytics / Beginner

Set

Definition

A set is a collection of distinct elements. In this entry, the source-list meaning is Python's set type: a mutable collection of hashable objects with no promised positional or insertion order. It is useful when membership matters more than repetition or sequence.

What it means

Imagine comparing the customer identifiers in two files. You may want to know which identifiers occur in both, which appear only in one, or how many different identifiers occur altogether. A set expresses those questions directly.

Turning a list into a set discards repeated equal elements. That is a change in the information retained, not simply a different display. A list of five purchases by one customer and a list of one purchase by that customer produce the same customer-ID set.

The mathematical idea of membership underlies this programming structure, but Python also imposes practical rules. A set does not identify its first or third member. An order seen when printing it must not become an unstated reporting assumption.

How it works

Create an empty Python set with set(); empty braces create a dictionary. A nonempty literal such as {'red', 'blue'} creates a set. Membership uses the in operator. The ordinary set type supports adding and removing members, while frozenset provides an immutable alternative.

Elements must be hashable: their hash and equality behavior must obey the requirements used for lookup. Strings and integers are common members; ordinary lists and dictionaries cannot be members directly. A tuple containing a list is not made hashable merely by being a tuple.

Union combines membership, intersection retains shared members, and difference keeps members of the first collection absent from the second. Symmetric difference retains members present in exactly one collection. A subset test asks whether every member of one set occurs in another.

Select the representation before applying these operations. A customer name is often a weaker matching key than a stable customer identifier.

Three practical examples

  • A training coordinator compares enrolled employee IDs with completion IDs to find enrolled people without a recorded completion.
  • An AI evaluation team checks whether document IDs overlap between training and test collections, then investigates any shared provenance.
  • A household inventory tool lists distinct product categories, while preserving the original purchase records separately for quantities and expenditure.

Think of it this way

Think of a guest checklist containing one tick box per recognized guest. A guest either appears on the checklist or does not; entering their name twice should not create two distinct guests.

The analogy has limits. Software does not recognize people by understanding their identities. It compares the supplied values under defined equality rules. Two accounts belonging to one person can remain two members, while two different people sharing an incorrectly reused identifier can collapse into one.

Worked example

Comparing attendance lists

Consider invented registration records ['A', 'A', 'B', 'C'] and attendance records ['B', 'C', 'C', 'D']. Their sets are R = {'A', 'B', 'C'} and T = {'B', 'C', 'D'}.

R intersect T contains B and C: two registered identifiers also attended. R minus T contains A: one registered identifier has no attendance match. T minus R contains D: one attendance identifier lacks a registration match. The union contains four distinct identifiers.

The original lists each contain four records, but each set contains three members. Neither set reveals how many times C was scanned or why A was registered twice. Those questions require the original records or a frequency counter. Sorting the resulting identifiers for a report creates an explicit presentation order; it does not restore their original sequence.

Illustrative case study

Fictional case: Cedar Learning builds an assistant that summarizes course participation. Its first version converts every attendance row to a set of names and reports the size as the number of learners.

A review finds two different learners called Sam Lee and one learner registered under two spelling variants. The team changes the matching key to a maintained learner ID, separates distinct participants from attendance events, and retains the source rows for investigation.

It also sorts the final display explicitly so that reviewers see a stable report. The correction addresses identity and reporting rules; using a set alone never guaranteed those rules. No performance improvement or business outcome is claimed for this invented case.

Common misconceptions and mistakes

  • Removing duplicate values proves the underlying records were duplicates. Equality of a chosen key may hide distinct real-world entities.
  • A set preserves the order in which items were received. Python sets provide no such ordering contract.
  • Set difference is symmetric. R minus T and T minus R generally answer different questions.
  • A set retains frequencies. Repetition is discarded; use counts when multiplicity matters.

Business use

Sets support reconciliation, access-list comparison, distinct-value checks and basic data preparation. They can make an intended question clearer than a long sequence of manual comparisons.

Before using a distinct count in a business dashboard, define the entity, identifier, time window and treatment of missing values. Check whether repeated records are errors or meaningful events. For large or shared datasets, a database operation may implement the same membership idea more appropriately than loading everything into a local Python set.

People also ask

Can a set contain another set?

An ordinary mutable set cannot be a member, but a suitable frozenset can.

Does unordered mean randomly sampled?

No. Iteration order is not a random sampling procedure and offers no sampling guarantee.

Is set conversion enough to prevent training-data leakage?

No. It can expose matching identifiers, but duplicated content under different identifiers and other leakage paths require separate checks.

Check your understanding

An order file contains customer IDs ['K', 'K', 'M']. After conversion to a set, can you conclude that the business received two orders?

Answer: No. The set contains two distinct customer identifiers. The original file has three records; whether those records represent three valid orders requires the order-level data and its rules.

Related terms

  • List
  • Dictionary
  • Data leakage
  • hashability
  • union
  • intersection
  • set difference
  • deduplication

Learn next

Read list and dictionary, then compare two small identifier collections using union, intersection and both directions of difference. Keep a separate frequency table and explain which questions cannot be answered after discarding repetition.

  • List
  • Dictionary

More in Data science and analytics

  • Sequence analysis in social sciences - Sequence analysis in the social sciences studies ordered histories of states or events to understand how social processes unfold.
  • Sequential pattern mining - Sequential pattern mining discovers recurring ordered patterns within a collection of sequences.
  • Seriation (statistics) - Seriation is the arrangement of objects into an order that reveals structure in their data or relationships.
  • Shallow Copy - A shallow copy creates a new outer object while retaining references to the objects held inside it.
  • Simple Random Sampling - Simple random sampling is a probability-sampling method.
  • Slice - A slice selects part of a sequence using a start position, a stopping boundary and an optional step. In Python, the usual notation is sequence[start:stop:step].

Sources

University of Chicago, glossary, Set: https://ds1.datascience.uchicago.edu/glossary.html. Python documentation, set types and hashability requirements: https://docs.python.org/3.13/builtins/stdtypes.html. Python tutorial, sets and explicit sorting: https://docs.python.org/3/tutorial/datastructures.html. Examples and the Cedar Learning case are original illustrations.

Previous: Session (web analytics) Next: Set balancing

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer