AI Glossary / Data science and analytics / Beginner

Shallow Copy

Definition

A shallow copy creates a new outer object while retaining references to the objects held inside it. For a Python list or dictionary, changing the new container itself can be independent, but changing a shared mutable object inside it can affect what both containers show.

What it means

Copying is about relationships between objects, not just whether two printouts look alike. Two containers can display identical contents while being different objects. They can also contain references to exactly the same inner list or dictionary.

A shallow copy is useful when the outer arrangement needs to change but the contained objects are intentionally shared or will not be mutated. The danger comes from expecting complete independence without examining the nesting.

Ordinary assignment is different. With b = a, Python gives another name to the same object; it does not create a new list. With b = a.copy() for a list, b is a different outer list, but its elements initially refer to the same objects as a's elements.

How it works

Consider a list containing two inner lists. The outer list stores references to those lists. Its shallow copy creates another outer list and copies the references rather than recursively reproducing every reachable object.

Appending a new element to the copied outer list does not append it to the original outer list. Replacing one of the copied list's element references also leaves the corresponding reference in the original unchanged. However, appending to an inner list that both containers reference changes that shared inner object.

For built-in lists, copy() and a full list slice produce shallow copies. The copy module provides a generic copy.copy() operation. Deep copying instead recursively copies supported contents, with bookkeeping for repeated references and cycles; classes can customize copying behavior.

A deep copy is not automatically the right answer. Some resources cannot meaningfully be duplicated, and some relationships are intended to remain shared. State the required independence first.

Three practical examples

  • An analyst copies a list of immutable record identifiers before rearranging their order, leaving the original order intact.
  • An AI application creates a trial configuration from a dictionary but discovers that a nested list of enabled tools remains shared.
  • A household planner copies a weekly schedule's outer list, then deliberately shares read-only activity descriptions while replacing whole daily entries.

Think of it this way

Think of photocopying a directory of locker numbers. The new directory is a separate sheet, so crossing out a number on it does not alter the original sheet. Both directories can still point to the same lockers.

Changing what is inside one shared locker is visible to people following either directory. This analogy explains references and containers, but software copying is type-dependent. It does not imply that every object behaves like a locker or that every operation copies a directory.

Worked example

Following two different changes

Start with original = [[10, 20], [30, 40]] and create trial = original.copy(). The outer lists are separate; the two inner lists are initially shared.

First perform trial[0].append(99). Both original[0] and trial[0] now show [10, 20, 99], because the operation changed their shared inner list. The original outer list was not replaced, yet its displayed contents changed through that relationship.

Next perform trial[1] = [50, 60]. This replaces only the second reference in trial. Original[1] still shows [30, 40], while trial[1] shows [50, 60].

Finally, trial.append([70]) adds a third element only to trial. These three operations distinguish mutation of a shared inner object, replacement of an outer reference, and extension of the copied container. Simply saying that copies are either independent or linked misses that distinction.

Illustrative case study

Fictional case: Birch Assist stores a baseline configuration containing a nested list of permitted tools. A developer makes a shallow copy for an experiment and appends a tool to the copied configuration's nested list.

The baseline now exposes the extra tool too, because both configurations reference that list. A test that checked only whether the outer dictionaries were different missed the problem.

The team writes down which settings must remain independent, constructs a separate tools list for the experiment and tests the resulting behavior. It also considers an immutable representation for approved settings. The lesson is to copy according to the intended ownership of data, not to assume that a method named copy creates complete isolation.

Common misconceptions and mistakes

  • A new outer object means everything inside is new. Shallow copying preserves references to contained objects.
  • Any change to a shallow copy affects the original. Changes to its separate outer structure need not do so.
  • Assignment is a shallow copy. Ordinary assignment creates a binding to the same object.
  • Deep copying is always safer. It can duplicate relationships that should remain shared and cannot clone every external resource.

Business use

Copy behavior matters in reusable templates, experiment settings, cached records and automated workflows. An unintended shared list can make one customer's configuration alter another's or make an experiment change its baseline.

Review where mutable objects are created, shared and changed. For simple configurations, explicitly constructing the independent fields may be clearer than recursively copying a complex object graph. Test a representative nested mutation as well as outer-container replacement. A printout immediately after copying cannot establish independence because the values are expected to match then.

People also ask

Are immutable elements a problem?

Their values cannot be mutated in place, so sharing them is often appropriate. The outer container may still be mutable.

Does every library's copy method mean shallow copy?

No. Libraries define their own behavior; consult the relevant type's documentation.

Is a shallow copy a backup?

It is not a durable backup and does not necessarily preserve nested state against later mutation.

Check your understanding

A copied dictionary and its original both reference the same list under the key alerts. The copied dictionary replaces that value with a completely new list. Must the original dictionary's alerts value change?

Answer: No. Replacing the copied dictionary's reference is different from mutating the shared list. The original still refers to the previous list unless another operation changes it.

Related terms

  • Deep Copy
  • Aliasing (factorial experiments)
  • List
  • Dictionary
  • object reference
  • mutable object
  • immutable object
  • aliasing
  • ownership

Learn next

Read mutable object and deep copy. Draw two outer containers pointing to one inner list, then predict the result of appending to the inner list versus replacing one outer reference. Verify those predictions with a small, disposable example.

  • Deep Copy
  • List

More in Data science and analytics

  • 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.
  • Set - A set is a collection of distinct elements.
  • 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].
  • SMOTE - SMOTE stands for Synthetic Minority Over-sampling Technique.

Sources

University of Chicago, glossary, Shallow Copy: https://ds1.datascience.uchicago.edu/glossary.html. Python documentation, assignment and shallow/deep copying: https://docs.python.org/3/library/copy.html?highlight=deepcopy. Python tutorial, list copying and data structures: https://docs.python.org/3/tutorial/datastructures.html. The numerical example and Birch Assist scenario are original illustrations.

Previous: Shadow removal Next: Shallow learning

  • 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