A list is an ordered, mutable collection of items. Lists can contain any type of data and are one of Python's most versatile data structures.
Lists use zero-based indexing. Use negative indices to access from the end:
Use [start:stop:step] to extract parts of a list:
colors[1:3] → ['green', 'blue']
colors[:2] → ['red', 'green']
colors[::2] → ['red', 'blue'] (every 2nd item)
colors[::-1] → ['yellow', 'blue', 'green', 'red'] (reversed)
Lists are mutable - you can change their contents:
Practice creating and accessing list elements.
Create a list called scores with values [85, 92, 78, 95, 88]. Print the first score, last score, and the middle three scores using slicing.
You know how to create and access lists. Let's learn list methods!
Python lists come with built-in methods for adding, removing, and manipulating elements.
list.count(x) - Count occurrences of x
list.index(x) - Find index of first x
list.copy() - Create a shallow copy
x in list - Check if x exists (returns True/False)
Practice using list methods to manipulate data.
Start with tasks = ["email", "meeting", "lunch"]. Add "report" to the end, insert "coffee" at the beginning, remove "lunch", and print the final sorted list.
You can manipulate lists like a pro. Now let's learn about tuples!
A tuple is like a list, but immutable - once created, it cannot be changed. Tuples are faster than lists and are used for data that shouldn't be modified.
You cannot modify a tuple after creation:
person[1] = 30 → TypeError: 'tuple' object does not support item assignment
If you need to "modify" a tuple, create a new one instead.
| Use Tuples When... | Use Lists When... |
|---|---|
| Data shouldn't change (coordinates, RGB values) | Data will be modified (shopping cart, scores) |
| Returning multiple values from functions | You need to add/remove elements |
| Dictionary keys (must be immutable) | Order matters and data is dynamic |
| Performance matters (tuples are faster) | You need list methods like sort, append |
Practice tuple unpacking - a powerful Python feature!
Given student = ("Bob", 21, "Computer Science", 3.8), unpack it into variables name, age, major, gpa and print a formatted message.
You know when to use tuples. Now let's explore sets!
A set is an unordered collection of unique elements. Sets automatically remove duplicates and provide fast membership testing.
set.add(x) - Add element x
set.remove(x) - Remove x (error if not found)
set.discard(x) - Remove x (no error if not found)
x in set - Fast membership test (O(1) average)
set.issubset(other) - Check if set ⊆ other
Use set operations to analyze data.
Given python_students = {"Alice", "Bob", "Charlie"} and java_students = {"Bob", "David", "Eve"}, find: students in both classes, students only in Python, and all unique students.
You understand sets. Let's learn list comprehensions!
List comprehensions are a concise way to create lists. They combine loops and conditions into a single line - very Pythonic!
[expression for item in iterable]
With condition: [expression for item in iterable if condition]
The expression is evaluated for each item that passes the condition.
Convert traditional loops to list comprehensions.
Create a list comprehension that generates the first 10 Fibonacci-like sequence starting with squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] (i.e., squares of 1-10).
You're writing Pythonic code now! Time for the summary.
Congratulations! You've mastered Python's core collection types. Let's recap:
| Type | Syntax | Ordered? | Mutable? | Duplicates? |
|---|---|---|---|---|
| List | [1, 2, 3] |
Yes | Yes | Yes |
| Tuple | (1, 2, 3) |
Yes | No | Yes |
| Set | {1, 2, 3} |
No | Yes | No |
Ordered, mutable. Use for collections that change.
Ordered, immutable. Use for fixed data, returns.
Unique elements, fast lookup. Use for deduplication.
One-line list creation. Pythonic and efficient.
You've mastered Python collections. Ready to prove your skills?