Course Progress24% complete
🐍 Python Basics Topic 24 / 100
⏱ 9 min read

Lists

Store multiple values in one variable — Python's most used data structure, explained from scratch with real examples.

"A variable holds one value. A list holds many. This one change unlocks an entirely new level of programming."

— ShurAI

The Problem Lists Solve

Suppose you are building a class marks tracker. Without a list, you create a separate variable for each student:

python — without lists (messy)
# ❌ A separate variable for each student — does not scale
student1_marks = 72
student2_marks = 88
student3_marks = 65
student4_marks = 91
# What if there are 40 students?? 😬
python — with a list ✅
# ✅ One variable, all values — works for 4 or 400
marks = [72, 88, 65, 91]

Creating a List

Square brackets [] with items separated by commas. Lists can hold any type — numbers, strings, booleans, or a mix:

python
# A list of fruits
fruits = ["mango", "banana", "apple", "guava"]

# A list of numbers
scores = [85, 92, 78, 96, 71]

# A list of mixed types (allowed but unusual)
mixed  = ["Riya", 22, True, 3.14]

# An empty list — ready to fill later
basket = []

print(type(fruits))   # <class 'list'>
print(len(fruits))    # 4 — number of items

Visualising a List

Think of a list as a row of labelled boxes. Each box has an item and a position number (index) starting at 0:

fruits = ["mango", "banana", "apple", "guava"]
"mango"
index 0
"banana"
index 1
"apple"
index 2
"guava"
index 3

Accessing Items by Index

python
fruits = ["mango", "banana", "apple", "guava"]

print(fruits[0])    # mango  — first item
print(fruits[2])    # apple  — third item
print(fruits[-1])   # guava  — last item
print(fruits[-2])   # apple  — second from last

Changing Items — Lists are Mutable

Unlike strings, lists can be changed after creation. You can update, add, or remove items:

python
cart = ["rice", "dal", "milk"]

# Change an item
cart[1] = "bread"
print(cart)      # ['rice', 'bread', 'milk']

# Add to the end
cart.append("eggs")
print(cart)      # ['rice', 'bread', 'milk', 'eggs']

# Remove an item by value
cart.remove("milk")
print(cart)      # ['rice', 'bread', 'eggs']

Looping Over a List

python
students = ["Riya", "Arjun", "Sneha", "Vikram"]

# Loop over items directly
for student in students:
    print(f"Hello, {student}!")

# Loop with index using enumerate()
for i, student in enumerate(students, 1):
    print(f"{i}. {student}")
output
1. Riya
2. Arjun
3. Sneha
4. Vikram

Useful List Functions

python
scores = [72, 88, 65, 91, 77]

print(len(scores))     # 5   — number of items
print(sum(scores))     # 393 — total
print(max(scores))     # 91  — highest
print(min(scores))     # 65  — lowest
print(sorted(scores))  # [65, 72, 77, 88, 91] — sorted copy

Real Example — Class Report Card 📊

python
names  = ["Riya", "Arjun", "Sneha", "Vikram", "Kavya"]
scores = [88,    72,     95,     61,      83   ]

print(f"{'Student':10} {'Score':>6}  Grade")
print("-" * 28)

for i in range(len(names)):
    s = scores[i]
    g = "A" if s >= 85 else "B" if s >= 70 else "C"
    print(f"{names[i]:10} {s:>6}   {g}")

print("-" * 28)
print(f"Average: {sum(scores)/len(scores):.1f}")
print(f"Top scorer: {names[scores.index(max(scores))]}")
output
Student        Score  Grade
----------------------------
Riya              88   A
Arjun             72   B
Sneha             95   A
Vikram            61   C
Kavya             83   B
----------------------------
Average: 79.8
Top scorer: Sneha

"Once you understand lists, everything becomes possible — student records, shopping carts, game inventories, data analysis. Lists are everywhere."

— ShurAI

🧠 Quiz — Question 1

How do you create a list in Python?

🧠 Quiz — Question 2

Given fruits = ["apple","mango","guava"], what is fruits[-1]?

🧠 Quiz — Question 3

What is the key difference between lists and strings regarding mutability?

🧠 Quiz — Question 4

What does len(["a","b","c","d"]) return?