AI Glossary / Data science and analytics / Beginner
Slice
Definition
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]. The stopping position is excluded. What the result shares with the original depends on the object's type.
What it means
Slicing lets a program express a regular positional selection compactly: the first few records, every second measurement or a sequence in reverse. It describes positions in an existing arrangement, rather than a condition such as “all orders above a given value.”
Python positions normally start at zero. In a list, index zero refers to the first element. A slice from position one up to position four therefore selects positions one, two and three, not four.
The source-list term concerns programming sequences. Business analysts also use “slice” more broadly for a selected portion of a dataset, such as one region. That wider usage should not be confused with Python's precise positional rules or with a random sample.
How it works
For a positive step, a slice begins at the start position and advances by the step while remaining before the stop boundary. Omitting the step normally means one. Omitting boundaries selects the appropriate beginning or end for the direction of travel.
Negative indices count relative to the sequence's end, and a negative step moves backwards. The expression values[::-1] reverses the selection order. A step of zero is invalid. Out-of-range slice boundaries are adjusted to the sequence bounds rather than behaving like an out-of-range single index.
For built-in lists, retrieving a slice produces a new outer list, with references to the selected elements. It is therefore a shallow copy. Basic slicing of a NumPy array generally produces a view sharing the underlying data instead.
Assigning to a list slice is also different from merely retrieving one: it changes the target list. Check the object type and intended operation before assuming independence.
Three practical examples
- An analyst previews the first ten records of an ordered list with records[:10], while treating the result as a preview rather than a representative sample.
- An AI training workflow partitions an already defined chronological sequence at a documented boundary, preserving the time order required by its evaluation design.
- A personal budgeting script extracts the last three entries from a list with entries[-3:], after confirming that the list is actually sorted by the intended date.
Think of it this way
Think of selecting numbered seats along a row, starting at one position and stopping before another. A step of two visits alternate positions rather than every seat.
The analogy helps with boundaries and spacing, but it does not explain memory ownership. In software, the selected result might be a new list or a view onto shared array data. Moving an imaginary seat cannot tell you which behavior a particular library implements.
Worked example
Reading the boundaries correctly
Let values = [10, 20, 30, 40, 50, 60]. Its positions are zero through five. The slice values[1:5:2] selects positions one and three, producing [20, 40]. Position five is the excluded stopping boundary, so 60 is not selected.
The slice values[-3:] selects the last three elements, giving [40, 50, 60]. The slice values[4:1:-1] selects positions four, three and two, giving [50, 40, 30]; it stops before position one.
Now set preview = values[:2]. Replacing preview[0] with 99 leaves values[0] equal to 10 because these are separate outer lists containing immutable integers. If the selected elements were mutable inner lists, mutating a shared inner list could still affect both. A corresponding NumPy basic slice would require a separate check because it normally shares array storage.
Illustrative case study
Fictional case: Alder Analytics builds an AI-assisted report from the last hundred records in a list. The team assumes this means the latest hundred events, but the upstream file is sorted by customer identifier.
The slice is technically correct and answers the wrong business question. The team establishes the event-time ordering, defines a tie rule and documents whether late-arriving events belong in the extract.
During testing, it also discovers that a NumPy slice used for temporary adjustments shares data with the original array. An explicit copy is introduced where independent numerical changes are required. The case shows two separate issues: selecting the right positions and understanding whether the selected data remains shared.
Common misconceptions and mistakes
- The stop index is included. Standard Python sequence slicing excludes it.
- The first index is one. Python's ordinary positional indexing starts at zero.
- Every slice is an independent deep copy. Built-in list slices are shallow, and NumPy basic slices usually share data.
- Every tenth row is a random sample. It is a deterministic selection unless embedded in an appropriate sampling design.
- Last means latest. That interpretation depends on the existing order.
Business use
Slicing supports previews, chunked processing, rolling selections and positional extraction. Its simplicity makes boundary mistakes easy to overlook, especially when records are split into training and evaluation groups.
Document whether boundaries represent positions, timestamps or labels, and avoid transferring rules from one library to another without checking. For a time-based evaluation, a positional cut is meaningful only after the ordering and group constraints are established. Related records spanning both sides can still cause leakage even when the slice boundaries themselves are correct.
People also ask
Does a slice have to select adjacent elements?
No. A step other than one can skip positions, and a negative step can reverse direction.
What happens if the starting position is beyond the end?
For an ordinary positive-step list slice, the result can simply be empty rather than raising an index error.
Can I use these rules unchanged for every table library?
No. Some APIs distinguish positional and label-based selection and use different endpoint conventions.
Check your understanding
A list is ['A', 'B', 'C', 'D', 'E']. What does the slice [1:4:2] return, and does it randomly select two records?
Answer: It returns ['B', 'D'], from positions one and three. The selection is determined by the existing order and slice parameters; it is not random sampling.
Related terms
- Sequence
- Index
- List
- Shallow Copy
- Sampling
- array view
- NumPy
- train-test split
Learn next
Read shallow copy and array view. Write down the positions in a six-element sequence, predict three positive and negative-step slices, and then test whether changing a retrieved result changes the original for the specific object type you use.
More in Data science and analytics
- 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.
- SMOTE - SMOTE stands for Synthetic Minority Over-sampling Technique.
- Social data science - Social data science combines computational and statistical methods with social-science theory and research design to study people, relationships...
- Social media mining - Social media mining is the use of computational and analytical methods to discover patterns in social-media content, activity and relationships.
Sources
University of Chicago, glossary, Slice: https://ds1.datascience.uchicago.edu/glossary.html. Python documentation, sequence slicing: https://docs.python.org/3/library/stdtypes.html?highlight=discard. NumPy, Indexing on ndarrays: https://numpy.org/doc/2.0/user/basics.indexing.html. NumPy, Copies and views: https://numpy.org/doc/stable/user/basics.copies.html. Examples and the Alder Analytics case are original illustrations.
Previous: Slack Next: Slice sampling