Coding/Learn/python/lists
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

LearnLists & Mutable Sequences
21 / 32 0%
Lesson 21 of 32
7 mins read

Lists & Mutable Sequences

PreviousNext Lesson
In Plain English

A Python list is like your grocery list scribbled on paper: items have a defined order, you can add more items at the bottom with .append(), cross items off, or swap things out at will. Defined with square brackets [].

The Mental Model

"Imagine an organized spice rack where each jar has a slot number. You can add new spices to the rack, swap paprika for garlic, or count how many spices you have in total."

Deep Dive: How It Works

Lists are ordered, mutable (changeable) sequences that can store any mix of data types (integers, strings, floats, booleans, and even nested lists).

Creating lists: Defined with square brackets: fruits = ["apple", "banana", "cherry"]. An empty list is created with [] or list().

Accessing elements: Lists are 0-indexed. fruits[0] gives "apple", fruits[1] gives "banana". Negative indices count backward from the end: fruits[-1] gives the last item ("cherry").

Common methods: .append(item) adds to the end, .insert(index, item) inserts at a specific position, .remove(item) deletes by value, and len(list) returns the total item count.

Syntax Blueprint

items = ["A", "B", "C"]
items.append("D")       # Adds "D" to the end
first = items[0]        # "A"
last = items[-1]        # "D"

Use square brackets [] for creation and indexing. Indices start at 0.

PointCore Rules to Remember

Point star
Square Brackets []: Lists are written inside square brackets separated by commas.
Point star
Fully Mutable: You can modify, append, sort, and remove list items at any time.
Point star
Zero-Indexed: The first item is always at index 0, the last item is at index -1.

Common Beginner Traps & How to Fix Them

Mistake:IndexError: list index out of range

Why it happens: Trying to access index 3 in a 3-item list (which only has indices 0, 1, 2).

How to fix: Remember that the last index is always len(my_list) - 1 or use my_list[-1].

Pro Tip: Check if an item exists in a list instantly with the "in" keyword: if "apple" in fruits:.

Live Interactive Example

Hit Run Code to see it live
Building and Modifying a List
Python 3.12
1
2
3
4
5
6
7
8
Output Console
Click "Run Code" to view the rendered output.
How it works: len() returns the element count, while .append() dynamically attaches a new item to the end.

Your Turn: Micro Challenge

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

Micro Exercise

Append an element to a list

  • PointCreate a list colors = ["red", "blue"].
  • PointAppend "green" to it and print the updated colors list.
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