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?
Question 2: What index accesses the last element of a list using negative indexing?
Question 3: How can you check the total number of items in a list?
Question 4: What is the output of values = [10, 20, 30]; print(values[3])?
Question 5: What is the result of nums = [1, 2, 3, 4, 5]; print(nums[1:3])?
Question 6: What is the default start index in a slice expression like list[:3]?
Question 7: What does the list method .append() do?
Question 8: How can you insert an item at the beginning of a list named items?
Question 9: What is the output of values = [1, 2, 2, 3]; values.remove(2); print(values)?
Question 10: What does values.sort() return when executed?
🖥️ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
vals = [10, 20, 30]
print(vals[1])
Predict the output of the code snippet below. Type the exact output in the console.
vals = [1, 2]
vals.append(3)
print(vals)
Predict the output of the code snippet below. Type the exact output in the console.
vals = [10, 20]
vals.insert(0, 5)
print(vals)
Predict the output of the code snippet below. Type the exact output in the console.
vals = [1, 2, 3]
x = vals.pop()
print(x)
Predict the output of the code snippet below. Type the exact output in the console.
vals = [3, 1, 2]
vals.sort()
print(vals)
🎮 Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
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
📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.