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.

3 min readUpdated 2026-11-29
In This Lesson
  • 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:

python
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)0123
Position (from right)-4-3-2-1
python
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])

List Indexes & Slice [1:4] in [10, 20, 30, 40, 50]
0
10
-5
1
20
-4
2
30
-3
3
40
-2
4
50
-1
Top: positive index (0 to 4)Bottom: negative index (-5 to -1)

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:

python
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:

python
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)

python
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 =:

python
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!
How to Make a Real Independent Copy

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

Python 3Try It Live: Playlist Editor
Output

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

Check Your Understanding

In colors = ['red', 'green', 'blue', 'yellow'], what does colors[1:3] return?

Check Your Understanding

What does list[-1] always give you?