Nested structures in Python allow us to combine data types like lists, dictionaries, and tuples to organize information efficiently. Think of them as containers within containers, like folders holding files or nested storage boxes. Let's explore:
Lists of Dictionaries
A list can hold multiple dictionaries, where each dictionary stores details about an item, person, or object. This is useful for organizing multiple records of similar data.
Imagine you’re tracking student information for a class. Each student has a dictionary of details, and all these dictionaries are stored in a list.
Example:
students = [
{"name": "Alice", "age": 20},
{"name": "Bob", "age": 22},
{"name": "Charlie", "age": 23}
]
Here:
- The list
contains three dictionaries.students
- Each dictionary represents a student’s information.
Accessing data:
print(students[0]) # Outputs: {'name': 'Alice', 'age': 20}
print(students[0]["name"]) # Outputs: Alice
Tuples within Lists
A tuple is a fixed, unchangeable collection of items. Combining tuples with lists helps you group related information while ensuring it stays unchanged.
Imagine you’re recording scores for students. Each record is a tuple with the student’s name and their score, stored inside a list.
Example:
scores = [
("Alice", 85),
("Bob", 90),
("Charlie", 88)
]
Here:
- The list
contains tuples.scores
- Each tuple pairs a student’s name with their score.
Accessing data:
print(scores[1]) # Outputs: ('Bob', 90)
print(scores[1][1]) # Outputs: 90
Dictionaries containing Lists or other Dictionaries
A dictionary can hold lists or even other dictionaries as its values. This is powerful for organizing related data in a structured way.
Dictionaries containing Lists
You can group multiple related items in a list and associate it with a key in a dictionary.
Imagine you’re recording a student’s name and their scores across different exams.
Example:
student = {
"name": "Alice",
"scores": [85, 90, 78]
}
Here, the student
"scores"
Accessing data:
print(student["name"]) # Outputs: Alice
print(student["scores"]) # Outputs: [85, 90, 78]
print(student["scores"][0]) # Outputs: 85
Dictionaries containing other Dictionaries
You can nest dictionaries inside another dictionary to represent hierarchical data.
Imagine you’re managing a school’s classes, where each class has a teacher and students.
Example:
school = {
"class_1": {
"teacher": "Mrs. Smith",
"students": ["Alice", "Bob"]
},
"class_2": {
"teacher": "Mr. Brown",
"students": ["Charlie", "David"]
}
}
Here:
- The
contains dictionaries for each class.school
dictionary - Each class dictionary stores its teacher and a list of students.
Accessing data:
print(school["class_1"]["teacher"]) # Outputs: Mrs. Smith
print(school["class_1"]["students"][1]) # Outputs: Bob
Why are nested structures useful?
Nested structures reflect real-world relationships and make your data more organized. For example:
- A shopping cart might have a list of product dictionaries.
- A university system might store courses (dictionaries) with enrolled students (lists) and grades (tuples).