📋 Day 5: Lists & Dictionaries

Storing Collections of Data

📚 What You'll Learn Today

Lists and dictionaries are the most common ways to store collections of data:

📋 Lists

A list is an ordered collection of items. You can think of it like a shopping list.

Creating a List:

# Empty list fruits = [] # List with items fruits = ["apple", "banana", "orange"] # Mixed types mixed = ["hello", 42, 3.14, True]

Accessing Items (Indexing):

fruits = ["apple", "banana", "orange"] print(fruits[0]) # First item: apple print(fruits[1]) # Second item: banana print(fruits[2]) # Third item: orange
apple banana orange
💡 Remember: Python uses zero-based indexing! The first item is at index 0, not 1.

Negative Indexing:

fruits = ["apple", "banana", "orange"] print(fruits[-1]) # Last item: orange print(fruits[-2]) # Second to last: banana

✏️ Modifying Lists

Changing Items:

fruits = ["apple", "banana", "orange"] fruits[0] = "mango" print(fruits)
['mango', 'banana', 'orange']

Adding Items:

fruits = ["apple", "banana"] # Append - add to end fruits.append("orange") print(fruits) # Insert - add at specific position fruits.insert(1, "grape") print(fruits)
['apple', 'banana', 'orange'] ['apple', 'grape', 'banana', 'orange']

Removing Items:

fruits = ["apple", "banana", "orange"] # Remove by value fruits.remove("banana") print(fruits) # Remove by index del fruits[0] print(fruits)
['apple', 'orange'] ['orange']

🔍 List Methods

Method Description Example
append() Add to end [].append(1)
insert(i, x) Insert at index [].insert(0, 1)
remove(x) Remove by value [].remove(1)
pop() Remove and return last [].pop()
sort() Sort in place [].sort()
len() Get length len([])

📖 Dictionaries

A dictionary stores data in key-value pairs. Like a real dictionary where you look up a word (key) to find its definition (value).

Creating a Dictionary:

person = { "name": "Alex", "age": 25, "city": "New York" }

Accessing Values:

person = {"name": "Alex", "age": 25} print(person["name"]) # Alex print(person["age"]) # 25
Alex 25

Adding/Modifying Values:

person = {"name": "Alex"} # Add new key person["age"] = 25 print(person) # Modify existing person["age"] = 26 print(person)
{'name': 'Alex', 'age': 25} {'name': 'Alex', 'age': 26}

🔑 Dictionary Methods

person = {"name": "Alex", "age": 25} # Get all keys print(person.keys()) # dict_keys(['name', 'age']) # Get all values print(person.values()) # dict_values(['Alex', 25]) # Get all items print(person.items()) # dict_items([('name', 'Alex'), ('age', 25)]) # Get with default print(person.get("country", "USA")) # USA (default)

🏋️ Exercise: Student Database

Create a program to manage student records using both lists and dictionaries!

# Create a list of student dictionaries # Each student should have: name, grade, subjects (list) # Add a student # Calculate average grade # Find the student with highest grade

Example Solution:

students = [ {"name": "Alex", "grade": 85}, {"name": "Jordan", "grade": 92}, {"name": "Taylor", "grade": 78} ] # Find highest grade top_student = max(students, key=lambda x: x["grade"]) print("Top student:", top_student["name"])

📊 Lists vs Dictionaries

Feature List [] Dictionary {}
Order ✅ Ordered ✅ Ordered (Python 3.7+)
Access by Index (number) Key (any type)
Use when Sequence of items Key-value lookups

✅ Key Takeaways

🎉 Ready for Day 6?

Tomorrow you'll learn about File Operations - reading and writing files!

Marking complete will unlock Day 6!