Coding/Learn/python/list indexing
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnList Indexing & Slicing
25 / 32 0%
Lesson 25 of 32
7 mins read

List Indexing & Slicing

PreviousNext Lesson
In Plain English

Indexing is like house numbers on a street, except computer scientists decided the first house starts at 0! Python also has a superpower: negative numbers count backward from the end, so list[-1] gets the very last item without having to calculate the length.

The Mental Model

"Think of slicing a loaf of bread. You can point to the first slice at [0], the crust at the end with [-1], or slice off a section from slice 2 to 4 using [2:5]."

Deep Dive: How It Works

Zero-Based Indexing: The first element is always at index 0, the second at 1, and the nth item at n-1.

Negative Indexing: Python lets you count from right to left using negative numbers. -1 is the last item, -2 is second-to-last, etc.

Slicing Syntax [start:stop:step]: Extracts a portion of the list from index start up to (but not including) index stop.

Omitted slice bounds: [:3] grabs everything from the start up to index 3. [2:] grabs everything from index 2 to the end. [::-1] creates a reversed copy of the entire list!

Syntax Blueprint

items[0]         # First item
items[-1]        # Last item
items[1:4]       # Slice items from index 1 to 3
items[::-1]      # Reverse the entire list

Use square brackets with colons for slicing [start:stop:step].

PointCore Rules to Remember

Point star
Zero-Based Counting: The first item is always at index [0], the second is at [1].
Point star
Negative Indexing [-1]: items[-1] gets the last item cleanly without calculating lengths.
Point star
Slicing [start:stop]: items[1:4] cuts out a sub-list from index 1 up to (not including) index 4.

Common Beginner Traps & How to Fix Them

Mistake:Off-by-one errors when slicing: items[0:3] for 4 items

Why it happens: Remember that the stop index is exclusive. To get 4 items (0, 1, 2, 3), you need stop index 4.

How to fix: Set the stop index to the desired length (e.g. items[:4]).

Pro Tip: Slicing works identically on strings: "Python"[0:2] gives "Py", and "Python"[::-1] gives "nohtyP".

Live Interactive Example

Hit Run Code to see it live
Slicing Up an Array of Rank Badges
Python 3.12
1
2
3
4
5
6
7
Output Console
Click "Run Code" to view the rendered output.
How it works: ranks[-1] extracts "Diamond" from the right, and [1:4] cuts out Silver, Gold, Platinum without altering the original array.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Extract last item with negative indexing

  • PointGiven scores = [88, 92, 79, 95], use negative indexing to print the last score.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates