Lists Part 1 — Creation and Basic Operations
Getting Started: Your First Lists
You already understand that lists are ordered, changeable sequences. Now let's learn how to actually create them and work with their elements. By the end of this lesson, you'll be able to create type-hinted lists and extract exactly the data you need using indexing and slicing.
Concept 1: Creating Lists — Literals and Constructors
There are two main ways to create a list in Python: list literals (using square brackets) and the list() constructor (converting data into a list).
List Literals: The Direct Way
The simplest way is to write a list literal with items inside square brackets:
Loading Python environment...
Notice the type hints like list[int] and list[str]. These tell you (and AI tools like type checkers) exactly what kind of items belong in the list. This is crucial for AI-native development—clear type hints communicate intent to both humans and tools.
💬 AI Colearning Prompt
"What happens if I try to add a string to a list[int]? Does Python stop me immediately or only when I use a type checker?"
This exploration helps you understand the difference between runtime behavior (what Python actually does when you run code) and static analysis (what type checkers like mypy flag as potential bugs). Both matter in professional code.
🎓 Expert Insight
In AI-native development, type hints are documentation that enable tools. When you write
numbers: list[int], you're saying "this list holds integers." Python won't stop you from adding a string at runtime, but a type checker will warn you, and your AI partner can catch errors before they cause problems.
List Constructor: Converting to a List
The list() constructor converts other collections into lists. Most commonly, you'll convert strings or ranges:
Loading Python environment...
Concept 2: Type Hints Express Intent
Type hints tell a story about your data. Compare these:
Loading Python environment...
Why this matters: Type hints act as executable documentation. When someone (or an AI tool) reads list[str], they immediately know what to expect.
🚀 CoLearning Challenge
Ask your AI Co-Teacher:
"Create a list of student names with a type hint. Then create another list of their GPAs. Now show me what type hint would represent 'a list of dictionaries where each dictionary has name and GPA.' Don't implement it—just show the type hint and explain it."
Expected Outcome: You'll understand how type hints scale from simple lists to complex nested structures. This prepares you for real-world data.
Concept 3: Index-Based Access — Getting Items
Every item in a list has a position called an index. Python uses zero-based indexing, meaning the first item is at index 0, the second is at index 1, and so on.
Positive Indexing: Counting from the Start
Loading Python environment...
Negative Indexing: Counting from the End
Python also supports negative indices, which count backward from the end:
Loading Python environment...
Negative indexing is incredibly useful when you want to access items near the end without knowing the list's length.
✨ Teaching Tip
Use Claude Code to explore index errors: "What happens if I try fruits[10] on a 4-item list? Show me the error and explain it." Understanding IndexError is crucial for writing defensive code.
💬 AI Colearning Prompt
"Why does Python use zero-based indexing instead of one-based (where the first item is 1)? What are the advantages and disadvantages of each approach?"
This deeper question helps you appreciate Python's design decisions—useful when you need to explain code to teammates.
Concept 4: Slicing — Getting Multiple Items
Slicing extracts a range of items from a list. The syntax is list[start:stop:step]:
- start: Index where the slice begins (inclusive, default 0)
- stop: Index where the slice ends (exclusive, default len(list))
- step: How many positions to jump (default 1)
Basic Slicing (No Step)
Loading Python environment...
Slicing with Negative Indices
Loading Python environment...
Slicing with Step (Every Nth Item)
Loading Python environment...
The [::-1] pattern is a Pythonic way to reverse any sequence!
🚀 CoLearning Challenge
Ask your AI Co-Teacher:
"I have a list of 10 numbers. Show me 5 different slice notations to get the middle 5 elements. Explain why each one works."
Expected Outcome: You'll understand that Python offers multiple ways to express the same operation. Choose the clearest one for your context.
🎓 Expert Insight
You don't memorize slice syntax. You understand: "I need items 2 through 5, so I write [2:5]." If you forget whether stop is inclusive or exclusive, ask your AI partner. The understanding—why you're slicing—matters far more than remembering the edge cases.
Concept 5: The len() Function — How Many Items?
The len() function returns the number of items in a list:
Loading Python environment...
You'll use len() constantly when you need to know how many items you're working with.
Concept 6: Aliasing vs Copying — A Critical Distinction
Here's a subtle but important concept: what happens when you assign one list to another variable?
Aliasing: Two Names, Same List
Loading Python environment...
Why? Both list1 and list2 are aliases—they point to the same list object in memory. When you modify through one, the other reflects the change.
Copying: Two Independent Lists
If you want a separate copy, use the .copy() method:
Loading Python environment...
This is a frequent source of bugs! You think you're modifying a copy, but you're actually modifying the original.
💬 AI Colearning Prompt
"Explain the difference between
list2 = list1andlist2 = list1.copy()using a memory diagram or analogy. Why would this bug be hard to catch?"
✨ Teaching Tip
Use Claude Code to explore aliasing: "Show me what happens when I modify list2 after
list2 = list1. Draw a memory diagram explaining why the original list changed." This visual understanding is worth more than memorizing the rule.
Practice: Apply What You've Learned
Exercise 1: Create Lists with Type Hints
Create lists for the following scenarios and write them with appropriate type hints:
- A list of your 5 favorite colors
- A list of numbers from 1 to 10
- An empty list that will eventually hold temperatures (as floats)
Exercise 2: Indexing Practice
Given this list:
Loading Python environment...
Write expressions to retrieve:
- The first animal
- The last animal
- The third animal
- The second-to-last animal
Exercise 3: Slicing Predictions
Given this list:
Loading Python environment...
Predict what each slice returns (don't run code—reason through it):
numbers[2:7]numbers[-4:]numbers[::3]numbers[1:8:2]numbers[::-1]
Exercise 4: Aliasing vs Copying
Predict what will be printed:
Loading Python environment...
Summary: What You Now Know
You can now create lists with type hints and extract exactly the data you need using indexing and slicing. You understand the critical difference between aliasing (pointing to the same list) and copying (creating a separate list). In the next lesson, you'll learn how to modify lists by adding and removing items.
Try With AI
Master list indexing, slicing, and the aliasing trap.
🔍 Explore Indexing Patterns:
"Show me how ['apple', 'banana', 'cherry', 'date', 'elderberry'] behaves with [0], [-1], [1:3], [:2], and [::2]. Explain each result and when I'd use these patterns."
🎯 Practice Slicing Operations:
"Help me extract data from a 20-student list: first 5 students, last 3 students, every other student, reversed list. Show slice notation for each."
🧪 Test Aliasing Behavior:
"Debug this code: original = [1, 2, 3]; backup = original; original.append(4). Why does backup become [1, 2, 3, 4]? Show how to create a true independent copy."
🚀 Apply to Data Extraction:
"Build a log analyzer that processes the last 100 entries from a list of 10,000 log messages, extracts every 10th entry for sampling, and creates independent backups. Demonstrate indexing and copying."