Thread Closed 
Thread Rating:
CHAPTER 12 — Lists: Storing Multiple Items
#1
Chapter 12 — Lists: Storing Multiple Items
Lists let you store many values in one variable — the foundation of real-world coding.

A list is like a container that holds multiple items:

Code:
fruits = ["apple", "banana", "cherry"]

Each item has an index:
• apple → index 0 
• banana → index 1 
• cherry → index 2 

---

12.1 Creating Lists

Code:
numbers = [10, 20, 30]
names = ["Mia", "Lee", "Johno"]
mixed = ["text", 42, 3.14, True]

Python allows mixed types — but good practice is to keep lists consistent.

---

12.2 Accessing Items

Use square brackets:

Code:
fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # apple
print(fruits[2])  # cherry

Indexes start at 0.

---

12.3 Changing Items

Code:
fruits[1] = "mango"
print(fruits)

---

12.4 Adding Items

append() → add to the end:

Code:
fruits.append("orange")

insert() → add to a specific position:

Code:
fruits.insert(1, "kiwi")

---

12.5 Removing Items

remove() → remove by value:

Code:
fruits.remove("banana")

pop() → remove by index:

Code:
fruits.pop(0)    # removes first item

Remove last item:

Code:
fruits.pop()

---

12.6 Checking Length

Code:
print(len(fruits))

---

12.7 Looping Through a List

Code:
for fruit in fruits:
    print(fruit)

---

12.8 Slicing Lists

Code:
numbers = [10, 20, 30, 40, 50]

print(numbers[1:4])  # [20, 30, 40]
print(numbers[:3])    # [10, 20, 30]
print(numbers[2:])    # [30, 40, 50]

---

12.9 Sorting Lists

Sort alphabetically or numerically:

Code:
numbers.sort()
names.sort()

Reverse:

Code:
numbers.reverse()

---

12.10 Checking for an Item

Code:
if "apple" in fruits:
    print("Apple is here!")

---

12.11 Copying Lists (Important)

If you want a REAL copy:

Code:
new_list = old_list.copy()

If you do this:

Code:
new_list = old_list

They become linked — changing one changes the other.

---

12.12 List of Lists

Lists can contain other lists:

Code:
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

Access:

Code:
print(matrix[1][2])  # 6

---

12.13 Mini Project — To-Do List

Write a program that:

• starts with an empty list 
• asks the user for 3 tasks 
• appends them 
• prints the final list 

---

12.14 Challenge — Shopping Cart System

Ask the user to enter items until they type “done”.

Store them in a list.

Then:

• display number of items 
• sort the list 
• print each item on a new line 

Example:

Code:
Your cart contains:
• apples
• bread
• milk

---

12.15 Chapter Summary

• lists store multiple values 
• indexes start at 0 
• append() adds to the end 
• remove() and pop() delete items 
• slices extract parts of lists 
• sort(), reverse(), and len() are essential 
• loops work beautifully with lists 
• lists can store ANY type 

Next:
Chapter 13 — Dictionaries: Storing Information With Keys

Dictionaries let you store data in a structured way, just like a database.

---

Written and Compiled by Lee Johnston — Founder of The Lumin Archive
« Next Oldest | Next Newest »
Thread Closed 


Forum Jump:


Users browsing this thread: 1 Guest(s)