← Back to Hub
Module 4 Week 11 • Creating, slicing, mutating, sorting, and indexing lists

Python Lists

Learn to store collections of items in lists, access them by index, slice sub-ranges, and modify contents in place.

1. Storing Multiple Values

A list is an ordered, mutable collection of items, written inside square brackets [] and separated by commas. Lists can store values of different data types together: E.g. info = ["Ann", 12, True].

2. Zero-Based & Negative Indexing

Each element in a list has a position index. Python uses zero-based indexing: the first item is at index 0, the second at 1, and so on. Negative indexing starts from the end of the list: the last item is at index -1, the second to last at -2.

3. Slicing Ranges

You can extract a sub-range from a list using slicing: list[start:stop:step]. Slicing returns a new list containing elements from the start index up to, but not including, the stop index. Slicing with a step of -1 ([::-1]) reverses the list!

4. List Mutations & Operations

Since lists are mutable, you can add and remove elements in place:

  • .append(item): Adds an item to the end of the list.
  • .insert(index, item): Inserts an item at the specified index, shifting others.
  • .pop(): Removes and returns the last item (or at a given index: .pop(0)).
  • .remove(item): Removes the first occurrence of a matching value.
  • .sort(): Sorts list items in-place (ascending order).

💻 Python Code Examples

Example 1: Creating Lists

marks = [45, 78, 92, 56, 80]
print(marks)
print(type(marks))

Description: Creates a list of integer values and checks its type class.

Example 2: Zero-Based and Negative Indexing

fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # First item
print(fruits[-1]) # Last item

Description: Accesses list elements using positive and negative indices.

Example 3: List Slicing Ranges

nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # Indices 1, 2, 3
print(nums[::-1]) # Reversed list

Description: Slices sub-ranges and demonstrates list reversal using step -1.

Example 4: Adding Items

colors = ["red", "blue"]
colors.append("green")
colors.insert(1, "yellow")
print(colors)

Description: Adds items to the end using append() and inserts at index 1 using insert().

Example 5: Removing Items

items = [10, 20, 30, 20]
items.remove(20) # Removes first 20
popped = items.pop()
print(items)
print("Popped item:", popped)

Description: Uses remove() to delete a specific value and pop() to extract the last element.

Example 6: Sorting In-Place

scores = [80, 50, 95, 75]
scores.sort()
print(scores)

Description: Applies sort() to organize numbers from smallest to largest in-place.

🧠 Active Retrieval Quizzes

Answer the following 10 multiple-choice questions to test your storage strength.

Question 1: What index represents the first item in a Python list?

Explanation: Python lists use zero-based indexing, so the first element is at index 0.

Question 2: What index accesses the last element of a list using negative indexing?

Explanation: Negative index -1 references the last item in a list.

Question 3: How can you check the total number of items in a list?

Explanation: The len() function returns the total number of elements in a list.

Question 4: What is the output of values = [10, 20, 30]; print(values[3])?

Explanation: Accessing an out-of-range index raises an IndexError.

Question 5: What is the result of nums = [1, 2, 3, 4, 5]; print(nums[1:3])?

Explanation: Slicing returns elements from start index 1 up to, but not including, stop index 3. This yields [2, 3].

Question 6: What is the default start index in a slice expression like list[:3]?

Explanation: If omitted, the slice start index defaults to 0.

Question 7: What does the list method .append() do?

Explanation: .append() adds a single element to the end of the list.

Question 8: How can you insert an item at the beginning of a list named items?

Explanation: insert(0, val) inserts the value at index 0 (the beginning of the list).

Question 9: What is the output of values = [1, 2, 2, 3]; values.remove(2); print(values)?

Explanation: The remove() method only removes the first matching value, leaving the second 2. Output: [1, 2, 3].

Question 10: What does values.sort() return when executed?

Explanation: sort() sorts the list in-place and returns None.

🖥️ Output Prediction Challenges

Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.

Challenge 1 of 5

Predict the output of the code snippet below. Type the exact output in the console.

vals = [10, 20, 30]
print(vals[1])
Console ready. Type your prediction below...
Challenge 2 of 5

Predict the output of the code snippet below. Type the exact output in the console.

vals = [1, 2]
vals.append(3)
print(vals)
Console ready. Type your prediction below...
Challenge 3 of 5

Predict the output of the code snippet below. Type the exact output in the console.

vals = [10, 20]
vals.insert(0, 5)
print(vals)
Console ready. Type your prediction below...
Challenge 4 of 5

Predict the output of the code snippet below. Type the exact output in the console.

vals = [1, 2, 3]
x = vals.pop()
print(x)
Console ready. Type your prediction below...
Challenge 5 of 5

Predict the output of the code snippet below. Type the exact output in the console.

vals = [3, 1, 2]
vals.sort()
print(vals)
Console ready. Type your prediction below...

🎮 Interactive Code Playground

Type your own Python code below and click "Run Code" to execute it instantly in your browser!

📝 Python Code Editor
Python WebAssembly runtime is unloaded. Click 'Run Code' to load.
🖥️ Output Console
Console ready.
💬 Teacher Consultation

Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.

📋 Progress Status

Lesson Complete?

📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.