Lesson 09
list — Mutable Ordered Collection
Learn Python's most commonly used collection type. Master list creation, indexing, slicing, mutability, duplicates, and building lists from other iterables.
Visual reference
What you will learn
- Create empty and non-empty lists
- Store mixed data types in one object
- Understand duplicates and insertion order
- Access values with indexing and slicing
- Modify lists in place (mutability)
Chapter 1 — What is a list?
list is a pre-defined collection type — one of the most flexible structures in Python. Values live inside square brackets [], separated by commas.
Purpose (your notes): store multiple values — same type, different type, or both — including unique and duplicate values, in a single object.
Definition: an ordered, mutable collection that can hold duplicate and mixed-type values.
Syntax: varname = [Val1, Val2, ..., Val-n]
| Property | list behavior |
|---|---|
| Order | Insertion order preserved |
| Index / slice | Yes (+ve and −ve indices) |
| Mutability | Mutable — item and slice assignment |
| Duplicates | Allowed |
my_list = [value1, value2, value3]Chapter 2 — Why use a list?
Imagine storing marks for five subjects.
Without a list — five separate variables:
math = 95
science = 88
english = 91
history = 84
computer = 99With a list — one variable holds everything:
marks = [95, 88, 91, 84, 99]Real-world examples
| Application | List example |
|---|---|
| Shopping cart | ["Milk", "Rice", "Eggs"] |
| Student marks | [95, 88, 91] |
| Property images | ["img1.jpg", "img2.jpg"] |
| User roles | ["Admin", "Editor"] |
Analogy: a shopping list on paper — you can change one line (mutability) and write the same item twice (duplicates).
Chapter 3 — Creating lists
Empty list
An empty list has length 0 and no elements.
lst1 = []
lst2 = list()
print(lst1)
print(type(lst1))
print(len(lst1))[]
<class 'list'>
0Non-empty list
numbers = [10, 20, 30, 40]
print(numbers)[10, 20, 30, 40]Your notes also use lists with duplicates and negatives: [10, 20, 30, 40, 10, 20, 45, 67, -56].
Chapter 4 — Lists can store mixed data types
Unlike fixed-type arrays in many languages, Python lists can hold different types together.
mixed = [
100,
"Rossum",
34.56,
True,
2 + 3j,
]
print(mixed)[100, 'Rossum', 34.56, True, (2+3j)]Chapter 5 — Duplicates are allowed
Lists preserve duplicates — they do not remove them automatically.
lst = [10, 20, 30, 10, 20]
print(lst)[10, 20, 30, 10, 20]Duplicates show up naturally in shopping carts, attendance logs, product quantities, and event streams. Converting a string to a list keeps repeated letters, e.g. list("MISSISSIPPI") → eleven characters including repeated S and I.
Chapter 6 — Indexing
Each position has an index. Index 0 is the first element; −1 is the last.
mixed = [100, "Rossum", 34.56, True, 2 + 3j]
print(mixed[0])
print(mixed[-1])100
(2+3j)Chapter 7 — Slicing
Syntax: list[start:stop] — stop is exclusive (same rule as range).
mixed = [100, "Rossum", 34.56, True, 2 + 3j]
print(mixed[0:4])[100, 'Rossum', 34.56, True]Reverse a list
print(mixed[::-1])[(2+3j), True, 34.56, 'Rossum', 100]Chapter 8 — Mutability (the superpower of lists)
Lists are mutable: you can change elements after creation.
mixed = [100, "Rossum", 34.56]
mixed[0] = 150[150, 'Rossum', 34.56]The same list object is updated. Your notes show id(lst1) unchanged after lst1[0] = 150 and after slice assignment — only the contents change.
Edit in the playground
Try mixed[0] = 150, slice updates, and compare id(mixed) before and after in the starter code.
Chapter 9 — Slice assignment
Replace multiple elements at once:
lst = [100, "Rossum", 34.56, True]
lst[1:3] = ["Guido", 55.55]
print(lst)[100, 'Guido', 55.55, True]Chapter 10 — Building lists from iterables
Syntax: listobj = list(object) when object is iterable (more than one value to loop over).
Syntax: listobj = list([object]) when you need a list around a single non-iterable value (int, float, etc.).
From a string
print(list("RADAR"))['R', 'A', 'D', 'A', 'R']From range
print(list(range(10, 20, 2)))[10, 12, 14, 16, 18]Why does this fail?
# list(10)
# TypeError: 'int' object is not iterableAn integer is not iterable — Python cannot loop through it. Fix: wrap the value: list([10]) when a = 10.
a = 10
print(list([a]))[10]Practice exercises
1. Extend a subject list
subjects = ["Math", "Science", "English"]
subjects = [*subjects, "Computer"]
print(subjects)['Math', 'Science', 'English', 'Computer']2. Reverse a list
nums = [1, 2, 3, 4]
print(nums[::-1])[4, 3, 2, 1]3. One integer inside a list
a = 10
print(list([a]))[10]Quick knowledge check
-
Are duplicates allowed in a list?
Yes. -
Can a list hold both integers and strings?
Yes. -
Does
lst[0] = 99modify the same list?
Yes — lists are mutable.
Summary
- A list is an ordered, mutable collection in
[]. - Insertion order and duplicates are preserved.
- Mixed types are allowed in one object.
- Use indexing and slicing to read; item and slice assignment to update in place.
- Build lists with
list(iterable)orlist([single_value])when needed.
Next lesson: tuple — immutable ordered collection (packing, unpacking, and when tuples beat lists).
Loading playground…