PYTHON FUNDAMENTALS: PROGRAMMING FOUNDATIONS / L05LISTS: YOUR FIRST DATA STRUCTURE
课程 · 12 · 05 / 12
LESSON 05 · BEGINNER · 45 MIN · ◆ 2 INSTRUMENTS

Lists: Your First Data Structure

Explore Python lists for storing collections of data. Learn indexing, slicing, and essential list methods.

TIP

Learning Objectives: After this lesson, you'll master Python lists for storing collections of data, understand indexing and slicing, and learn essential list methods for data manipulation.

What are Lists? The Train Analogy

Before diving into code, let's understand lists through a powerful analogy:

Lists are like trains - a series of connected cars carrying cargo.

Imagine a freight train traveling across the country:

🚂 The List Train

  • The train: Your Python list
  • Train cars: Individual items/elements in the list
  • Car numbers: Indices (0, 1, 2, 3...)
  • Cargo in each car: The actual data (numbers, strings, objects)
  • The engine: Python managing the list for you

Key Properties of the Train (List):

  1. Ordered: Cars stay in sequence (just like list items maintain order)
  2. Numbered: Each car has a position number starting from 0
  3. Flexible cargo: Each car can carry different types of cargo
  4. Changeable: Can add cars, remove cars, rearrange cargo
  5. Accessible: Can access any car directly by its number
🚂 List Train: colors = ["red", "green", "blue", "yellow"] Car 0 Car 1 Car 2 Car 3 ┌─────────┬─────────┬─────────┬─────────┐ │ "red" │ "green" │ "blue" │"yellow" │ └─────────┴─────────┴─────────┴─────────┘ Index: 0 1 2 3 Negative: -4 -3 -2 -1

Train Operations = List Operations:

  • Add a car: colors.append("purple") - attach new car at end
  • Insert a car: colors.insert(1, "orange") - couple car at specific position
  • Remove a car: colors.remove("green") - detach and remove specific car
  • Check a car: colors[2] - inspect cargo in car #2
  • Count cars: len(colors) - how many cars in the train?

What are Lists?

A list is Python's most versatile data structure - an ordered collection that can store multiple items of any type. Think of it as a container with numbered slots, where each slot can hold any Python value.

FIG. 02Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 02Interactive Python code execution environment

List Indexing

Each item in a list has an index (position number). Python uses zero-based indexing:

FIG. 04Data Structure Visualizer
INTERACTIVE
LOADING INSTRUMENT
Fig. 04Interactive visualization of Python data structures
FIG. 06Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 06Interactive Python code execution environment

List Slicing

Slicing lets you extract portions of a list using the syntax list[start:end:step]:

FIG. 08Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 08Interactive Python code execution environment

Essential List Methods

Lists come with many built-in methods for manipulation:

FIG. 10Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 10Interactive Python code execution environment

List Operations and Functions

FIG. 12Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 12Interactive Python code execution environment

Interactive List Visualization

Let's see how list operations work visually:

FIG. 14Data Structure Visualizer
INTERACTIVE
LOADING INSTRUMENT
Fig. 14Interactive visualization of Python data structures
FIG. 16Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 16Interactive Python code execution environment

List Comprehensions (Preview)

A powerful Python feature for creating lists:

FIG. 18Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 18Interactive Python code execution environment

Working with Lists of Different Types

FIG. 20Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 20Interactive Python code execution environment

Common List Patterns

FIG. 22Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 22Interactive Python code execution environment

Practical Examples

Example 1: Grade Tracker

FIG. 24Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 24Interactive Python code execution environment

Example 2: Shopping Cart

FIG. 26Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 26Interactive Python code execution environment

Practice Exercises

Exercise 1: List Manipulation

FIG. 28Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 28Interactive Python code execution environment

Exercise 2: Word List Processor

FIG. 30Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 30Interactive Python code execution environment

Key Takeaways

Lists are ordered collections - Items have specific positions (indices)
Zero-based indexing - First item is at index 0, last at index -1
Slicing syntax - list[start:end:step] extracts portions
Mutable - Lists can be modified after creation
Essential methods - append(), insert(), remove(), pop(), sort()
List comprehensions - Concise way to create and filter lists
Versatile - Can store any type of data, including other lists

Next Steps

In the next lesson, we'll explore dictionaries and sets - data structures that excel at storing key-value pairs and unique collections respectively.


Ready to learn about Python's most powerful data structures? The next lesson will introduce dictionaries and sets!


Further Reading

Visualize It

  • Python Tutor — paste any list code and watch how b = a shares a reference but b = a[:] creates a copy. The arrows make it instantly clear.
  • VisuAlgo — Sorting Algorithms — animated visualization of what list.sort() actually does under the hood (Timsort).

Official Docs

Tutorials

Performance & Modernization