Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner18 min

Visual reference

Shopping list — many items in one placeShopping list — many items in one place
Python lists overviewPython lists overview
List methods and operationsList methods and operations

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
Ordered collection [10, 20, 30, 40]
python
my_list = [value1, value2, value3]

Chapter 2 — Why use a list?

Imagine storing marks for five subjects.

Without a list — five separate variables:

python
math = 95
science = 88
english = 91
history = 84
computer = 99

With a list — one variable holds everything:

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

python
lst1 = []
lst2 = list()

print(lst1)
print(type(lst1))
print(len(lst1))
text
[]
<class 'list'>
0

Non-empty list

python
numbers = [10, 20, 30, 40]
print(numbers)
text
[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.

python
mixed = [
    100,
    "Rossum",
    34.56,
    True,
    2 + 3j,
]

print(mixed)
text
[100, 'Rossum', 34.56, True, (2+3j)]
One list — int, str, float, bool, complex

Chapter 5 — Duplicates are allowed

Lists preserve duplicates — they do not remove them automatically.

python
lst = [10, 20, 30, 10, 20]

print(lst)
text
[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.

Positive and negative indexing on mixed list
python
mixed = [100, "Rossum", 34.56, True, 2 + 3j]

print(mixed[0])
print(mixed[-1])
text
100
(2+3j)

Chapter 7 — Slicing

Syntax: list[start:stop] — stop is exclusive (same rule as range).

python
mixed = [100, "Rossum", 34.56, True, 2 + 3j]

print(mixed[0:4])
text
[100, 'Rossum', 34.56, True]

Reverse a list

python
print(mixed[::-1])
text
[(2+3j), True, 34.56, 'Rossum', 100]
[::-1] reverses the sequence

Chapter 8 — Mutability (the superpower of lists)

Lists are mutable: you can change elements after creation.

python
mixed = [100, "Rossum", 34.56]
mixed[0] = 150
text
[150, 'Rossum', 34.56]
Before index update
After mixed[0] = 150

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:

python
lst = [100, "Rossum", 34.56, True]

lst[1:3] = ["Guido", 55.55]

print(lst)
text
[100, 'Guido', 55.55, True]
Slice assignment lst[1:3]

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

python
print(list("RADAR"))
text
['R', 'A', 'D', 'A', 'R']

From range

python
print(list(range(10, 20, 2)))
text
[10, 12, 14, 16, 18]

Why does this fail?

python
# list(10)
# TypeError: 'int' object is not iterable

An integer is not iterable — Python cannot loop through it. Fix: wrap the value: list([10]) when a = 10.

python
a = 10
print(list([a]))
text
[10]

Practice exercises

1. Extend a subject list

python
subjects = ["Math", "Science", "English"]
subjects = [*subjects, "Computer"]
print(subjects)
text
['Math', 'Science', 'English', 'Computer']

2. Reverse a list

python
nums = [1, 2, 3, 4]
print(nums[::-1])
text
[4, 3, 2, 1]

3. One integer inside a list

python
a = 10
print(list([a]))
text
[10]

Quick knowledge check

  1. Are duplicates allowed in a list?
    Yes.

  2. Can a list hold both integers and strings?
    Yes.

  3. Does lst[0] = 99 modify 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) or list([single_value]) when needed.

Next lesson: tuple — immutable ordered collection (packing, unpacking, and when tuples beat lists).

Loading playground…