3. Lists, Tuples, Sets & References
Lists Deep Dive: Indexing, Slicing & Editing
Store multiple items together in a Python list: understand 0-based counting, negative indexes, slicing [start:stop], adding/removing items, and safely copying lists.
- Store and access ordered collections using 0-based and negative indexes
- Extract sub-lists cleanly with slicing ([start:stop])
- Add, edit, remove, and sort list items safely
Imagine building a to-do app. A user might have 3 tasks today and 15 tasks tomorrow. Creating variables task1, task2, task3... does not work when the count keeps changing.
Instead, Python lets you store any number of items inside a single container called a list.
Creating a List & Picking Items by Position
Write a list using square brackets [...], separating items with commas:
tasks = ["Buy milk", "Study Python", "Call mom", "Walk dog"]Now comes one weird rule every programmer learns on Day 1: Python starts counting positions at 0, NOT 1!
| Item | "Buy milk" | "Study Python" | "Call mom" | "Walk dog" |
|---|---|---|---|---|
| Position (from left) | 0 | 1 | 2 | 3 |
| Position (from right) | -4 | -3 | -2 | -1 |
print(tasks[0]) # "Buy milk" (first item)
print(tasks[1]) # "Study Python" (second item)
print(tasks[-1]) # "Walk dog" (last item—works even if you don't know the length!)
print(len(tasks)) # 4 (how many items are inside)Taking a Slice (list[start:stop])
Highlighted items [20, 30, 40] live at indexes 1, 2, and 3—stopping right before index 4.
What if you want items 1 and 2 together instead of just one? Use a slice [start:stop]. Just like range(), Python starts at start and stops right before stop:
nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # [20, 30, 40] (positions 1, 2, 3 — stops before 4!)
print(nums[:3]) # [10, 20, 30] (leave start empty -> starts from 0)
print(nums[2:]) # [30, 40, 50] (leave stop empty -> goes to the end)Changing, Adding & Removing Items
Unlike strings, lists can be edited anytime:
cart = ["Apple", "Bread"]
cart[1] = "Bagel" # Replace position 1 -> ["Apple", "Bagel"]
cart.append("Coffee") # Add to the end -> ["Apple", "Bagel", "Coffee"]
cart.insert(0, "Juice") # Insert at pos 0 -> ["Juice", "Apple", "Bagel", "Coffee"]
cart.remove("Apple") # Remove by name -> ["Juice", "Bagel", "Coffee"]
last = cart.pop() # Remove & return the last item ("Coffee")
print("Cart:", cart) # ["Juice", "Bagel"]
print("Removed:", last) # "Coffee"Checking If an Item Exists (in)
if "Juice" in cart:
print("Juice is already in your cart!")One Surprising Trap (b = a vs b = a.copy())
Look carefully at what happens if you copy a list using =:
a = [1, 2, 3]
b = a # b does NOT make a fresh copy—both labels point to the SAME list!
b.append(4)
print("a:", a) # [1, 2, 3, 4] <-- a changed too!Whenever you want a separate copy of a list so editing one doesn't change the other, write b = a.copy().
Try It Yourself in the Playground
Click “Run Code” to execute this snippet right in your browser.
Mini-Challenge: Given scores = [55, 92, 78, 88, 64], use sorted(scores, reverse=True) and slicing [:3] to print the top 3 highest scores ([92, 88, 78]).
Quick Checkpoints
In colors = ['red', 'green', 'blue', 'yellow'], what does colors[1:3] return?
What does list[-1] always give you?