📚 What You'll Learn Today
Lists and dictionaries are the most common ways to store collections of data:
Lists - ordered collections of items
Accessing and modifying list items
List methods (append, remove, etc.)
Dictionaries - key-value pairs
When to use each type
📋 Lists
A list is an ordered collection of items. You can think of it like a shopping list.
Creating a List:
fruits = []
fruits = ["apple" , "banana" , "orange" ]
mixed = ["hello" , 42 , 3.14 , True ]
Accessing Items (Indexing):
fruits = ["apple" , "banana" , "orange" ]
print (fruits [0 ])
print (fruits [1 ])
print (fruits [2 ])
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 ])
print (fruits [-2 ])
✏️ Modifying Lists
Changing Items:
fruits = ["apple" , "banana" , "orange" ]
fruits [0 ] = "mango"
print (fruits )
['mango', 'banana', 'orange']
Adding Items:
fruits = ["apple" , "banana" ]
fruits .append ("orange" )
print (fruits )
fruits .insert (1 , "grape" )
print (fruits )
['apple', 'banana', 'orange']
['apple', 'grape', 'banana', 'orange']
Removing Items:
fruits = ["apple" , "banana" , "orange" ]
fruits .remove ("banana" )
print (fruits )
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" ])
print (person ["age" ])
Alex
25
Adding/Modifying Values:
person = {"name" : "Alex" }
person ["age" ] = 25
print (person )
person ["age" ] = 26
print (person )
{'name': 'Alex', 'age': 25}
{'name': 'Alex', 'age': 26}
🔑 Dictionary Methods
person = {"name" : "Alex" , "age" : 25 }
print (person .keys ())
print (person .values ())
print (person .items ())
print (person .get ("country" , "USA" ))
🏋️ Exercise: Student Database
Create a program to manage student records using both lists and dictionaries!
Example Solution:
students = [
{"name" : "Alex" , "grade" : 85 },
{"name" : "Jordan" , "grade" : 92 },
{"name" : "Taylor" , "grade" : 78 }
]
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
Lists [] store ordered collections
Access items by index: list[0]
Add with append(), insert()
Remove with remove(), pop()
Dictionaries {} store key-value pairs
Access by key: dict["key"]
Add/modify by assigning: dict["key"] = value
Use lists for sequences, dictionaries for lookups
🎉 Ready for Day 6?
Tomorrow you'll learn about File Operations - reading and writing files!
✓ Mark Day 5 Complete
Marking complete will unlock Day 6!