Master Python List of Dictionaries

Deadline: October 11, 2024
Activity Type: Coding
Goal: Understand and apply the list of dictionaries data structure for various data sets.


Overview:

In this activity, you will practice using List of dictionaries to store and manage multiple instances of objects like Product, Employee, Books, University, and Restaurant. You will create dictionaries for these objects and populate them with details (e.g., names, prices, job titles). This activity is designed to help you understand how to manage collections of data in a structured manner.

This is a follow-up to Activity : Data Structure Again

Check this:

ACTIVITY (hashnode.space)


Instructions:

  1. Create the following Python classes:

    • Product: Store product details (e.g., product name, price).

    • Employee: Store employee details (e.g., name, job title).

    • Books: Store book details (e.g., title, author).

    • University: Store university details (e.g., name, location).

    • Restaurant: Store restaurant details (e.g., name, cuisine type).

  2. Use dictionaries to store the details of each class:

    • Create dictionaries for each object (e.g., list of product names, list of prices, list of employee job titles, etc.).

    • Populate the lists with at least 5 entries per object.

  3. Access and print the details from the lists.


Example Using Lists:

Below is an example of how you can structure your data using lists for students, similar to the data you provided in the image:

Example: Student Data Using Lists

Student.py:

To convert your individual student dictionaries into a list of dictionaries, you can structure it as follows:

Updated Code with List of Dictionaries:

# List of student dictionaries
students = [
    {"name": "Juan Carlos", "section": "BSIT-4B", "id": 1, "email": "juan@gmail.com", "age": 22},
    {"name": "Jose Rizal", "section": "BSIT-2A", "id": 2, "email": "jose@gmail.com", "age": 21},
    {"name": "Juan Luna", "section": "BSIT-3A", "id": 3, "email": "juanl@gmail.com", "age": 20},
    {"name": "Andres Bonifacio", "section": "BSIT-3A", "id": 4, "email": "andres@gmail.com", "age": 20},
    {"name": "Justin Bieber", "section": "BSIT-2A", "id": 5, "email": "justin@gmail.com", "age": 21},
    {"name": "Michael Jordan", "section": "BSIT-4A", "id": 6, "email": "michael@gmail.com", "age": 19},
    {"name": "Andrew Jordan", "section": "BSIT-4A", "id": 7, "email": "andrew@gmail.com", "age": 19},
    {"name": "Jessa Boe", "section": "BSIT-2B", "id": 8, "email": "jessa@gmail.com", "age": 18},
    {"name": "Ted Talk", "section": "BSIT-3B", "id": 9, "email": "ted@gmail.com", "age": 19}
]

# Print student data
for student in students:
    print(f"Name: {student['name']}, Section: {student['section']}, Email: {student['email']}")

Explanation:

  • The individual student dictionaries are now directly placed into a list (students).

  • You can access each student's data within the list using the loop.

  • No need to use .get() when accessing dictionary values unless you want to handle missing keys.


Tasks:

  1. Define a List of dictionaries for storing data related to the classes: Product, Employee, Books, University, and Restaurant.

  2. Populate the List of dictionaries with data for each class, ensuring there are at least 5 entries.

  3. Access and print the details of each object from the list.


Submission:


Updated on