Lists and Dictionaries (list · dict)
list (List)
Definition: A data type that stores multiple values in order, written with square brackets [].
Features
- Ordered (supports indexing)
- Mutable
- Can store different data types together
- Allows duplicate values
python
fruits = ["apple", "banana", "strawberry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "Jo", 3.14, True]
print(fruits[0]) # output: apple (index 0)
print(fruits[-1]) # output: strawberry (the last element)
print(len(fruits)) # output: 3 (list length)
# Modifying a list
fruits.append("grape") # add to the end
fruits.remove("banana") # remove "banana"
fruits[0] = "tangerine" # change the first element
print(fruits) # output: ['tangerine', 'strawberry', 'grape']dict (Dictionary)
Definition: A data type that stores data as key-value pairs, written with curly braces {}.
Features
- Order is not guaranteed (from Python 3.7 on, insertion order is preserved)
- Values are accessed by key (different from indexing)
- Keys cannot be duplicated, values can
- Mutable
python
student = {
"name": "Jo",
"age": 25,
"grade": "A",
"score": 95
}
print(student["name"]) # output: Jo
print(student["age"]) # output: 25
# Modifying/adding to a dictionary
student["age"] = 26 # modify
student["major"] = "Computer Science" # add
print(student.keys()) # output: dict_keys(['name', 'age', 'grade', 'score', 'major'])
print(student.values()) # output: dict_values(['Jo', 26, 'A', 95, 'Computer Science'])Summary
| Concept | Summary |
|---|---|
| list | Stores multiple values in order → managing multiple pieces of data |
| dict | Stores key-value pairs → pairing information systematically |
Reference: personal Notion study notes (onboarding course)
