Data Structures Again? 🤨

Data Structures Again? 🤨

For better understanding data structures we will add another explanations and example about how and why is it important to have a better knowledge in creating and manipulating data.

We can use this provided table example that is posted in the portal and let’s explain and give example to understand deeply the Key Differences

List: An ordered collection of individual product names.

Here’s how you can represent a list for product, with lists holding each product ID, product name, product category, product price, product stock, and product supplier email:

productNameList = ["Laptop", "Desk Chair", "Smartwatch", "Notebook", "Running Shoes"]
productCategoryList = ["Electronics", "Furniture", "Electronics", "Stationery", "Apparel"]
productPriceList = [750, 100, 200, 5, 80]
productStockList = [50, 200, 150, 500, 100]
productSupplierEmailList = [
    "supplier1@gmail.com",
    "supplier2@gmail.com",
    "supplier3@gmail.com",
    "supplier4@gmail.com",
    "supplier5@gmail.com"
]

Each list holds related information about various products. For example:

  • productNameList contains all the names of the product, such as "Laptop" and "Desk Chair."

  • productCategoryList categorizes each product, indicating whether it belongs to "Electronics," "Furniture," "Stationery," or "Apparel."

  • productPriceList lists the prices of each product, with corresponding values like 750 for a Laptop and 5 for a Notebook.

  • productStockList shows the available stock for each product, such as 50 for a Laptop and 500 for a Notebook.

  • productSupplierEmailList provides the contact emails of suppliers for each product.

You can access elements by their index. For example, productNameList[0] will give you "Laptop," and productPriceList[2] will give you 200, which is the price of the Smartwatch.

Lists: Useful when you need to manage multiple entries of the same type (like all product names or prices). You access them by index, such as productCategoryList[1] to get the category of the second product, which is "Furniture."

Object/Dictionary/Hashmap

An object allows us to store key-value pairs. Each piece of data (a student's name, section, etc.) is associated with a key.

Here are the individual product objects based on the data provided in the table image:

product_object1 = {
    "name": "Laptop",
    "category": "Electronics",
    "id": 1,
    "price": 750,
    "stock": 50,
    "supplier_email": "supplier1@gmail.com"
}

product_object2 = {
    "name": "Desk Chair",
    "category": "Furniture",
    "id": 2,
    "price": 100,
    "stock": 200,
    "supplier_email": "supplier2@gmail.com"
}

product_object3 = {
    "name": "Smartwatch",
    "category": "Electronics",
    "id": 3,
    "price": 200,
    "stock": 150,
    "supplier_email": "supplier3@gmail.com"
}

product_object4 = {
    "name": "Notebook",
    "category": "Stationery",
    "id": 4,
    "price": 5,
    "stock": 500,
    "supplier_email": "supplier4@gmail.com"
}

product_object5 = {
    "name": "Running Shoes",
    "category": "Apparel",
    "id": 5,
    "price": 80,
    "stock": 100,
    "supplier_email": "supplier5@gmail.com"
}

Objects/Dictionaries/HashMap: Better for holding related data about one entity (like a single product’s details). You access data by key, such as product_object1["name"] to get the name of the gadget.

List of Objects/Dictionaries/HashMap

We can also combine lists and objects to store multiple student records in a more organized way. A list of objects allows us to store a collection of student objects, each containing all the relevant details.

Now, you can store all these individual student objects into a list of objects:

products = [
    {
        "name": "Laptop",
        "category": "Electronics",
        "id": 1,
        "price": 750,
        "stock": 50,
        "supplier_email": "supplier1@gmail.com"
    },
    {
        "name": "Desk Chair",
        "category": "Furniture",
        "id": 2,
        "price": 100,
        "stock": 200,
        "supplier_email": "supplier2@gmail.com"
    },
    {
        "name": "Smartwatch",
        "category": "Electronics",
        "id": 3,
        "price": 200,
        "stock": 150,
        "supplier_email": "supplier3@gmail.com"
    },
    {
        "name": "Notebook",
        "category": "Stationery",
        "id": 4,
        "price": 5,
        "stock": 500,
        "supplier_email": "supplier4@gmail.com"
    },
    {
        "name": "Running Shoes",
        "category": "Apparel",
        "id": 5,
        "price": 80,
        "stock": 100,
        "supplier_email": "supplier5@gmail.com"
    }
]

List of objects allows us to manage multiple student records in a more organized manner. We can loop through the products list to access all the products data or filter it based on certain criteria (like finding all students in a particular section).

Again, this will give us another idea on how and why understanding the key differences between three important data structures: List, Object, and List of Objects.

List: A list is useful when you want to store a collection of similar items. In the products example, the list contains only the names of the products, where each name is accessed by its index.

product_names = ["Laptop", "Desk Chair", "Smartwatch"]

Why use a list? It's simple and efficient when you only care about one property of the product (in this case, the names).

Object (Dictionary): An object (or dictionary in Python) is useful when you need to store multiple related attributes for one entity. Here, we store details of a single product (like name, category, price, etc.) in key-value pairs.

product_object = {
    "name": "Laptop",
    "category": "Electronics",
    "price": 750,
    "stock": 50,
    "supplier_email": "supplier1@gmail.com"
}

Why use an object? It allows you to group all information about a single product in one place, making it easy to access or modify any property (e.g., price or stock).

List of Objects: A list of objects allows you to store details for multiple products, with each object in the list representing one product. This is ideal when you need to keep track of many entities, each with its own set of properties.

products = [
    {"name": "Laptop",
     "category": "Electronics",
     "price": 750},
    {"name": "Desk Chair",
     "category": "Furniture",
     "price": 100}
]

In summary, lists are useful for storing collections of similar values, while objects are ideal for grouping related properties for a single item. A list of objects combines both, allowing you to manage multiple items, each with its own set of detailed attributes.

Â