Skip to main content

Command Palette

Search for a command to run...

Activity 17: Data Structure Again

Published
•12 min read•View as Markdown

List, Object, and List of Objects

  1. List
    A list is a structured sequence of elements where each item has a defined position, and the data type of the elements can vary. These items could be integers, strings, or even more complex data types, such as other lists. Lists are flexible and allow for duplicate entries, meaning you can have the same element appear more than once. One of their most important features is mutability, which means the list can be altered—items can be added, removed, or updated after it has been created.
    Key Characteristics:

    • Ordered: The sequence of elements is maintained, so each item has a specific position.

    • Mutable: Lists can be modified, allowing you to change, add, or remove elements at any time.

    • Indexed: Each item is associated with an index, starting from 0, which makes it easy to access elements based on their position.

    • Allows Duplicates: The same element can appear multiple times, enabling redundancy when necessary.

This flexibility makes lists a powerful data structure for handling ordered collections of data that need to be accessed or modified frequently.

  1. Object

    An object is a data structure that consists of pairs of keys and values, often referred to as properties or attributes. In an object, each key must be unique, allowing it to be directly linked to a specific value, which can vary in data type. Objects are particularly useful for modeling real-world entities and encapsulating their associated characteristics.

    Key Characteristics:

    • Key-Value Pairs: Each property within an object is defined by a unique key that corresponds to its associated value, making it easy to retrieve specific information.

    • Mutable: The values of the properties in an object can be modified, allowing for updates and changes to the data it holds.

    • Unordered: Unlike lists, the arrangement of key-value pairs in an object is not fixed; hence, there is no guarantee of their sequence.

This structure provides a versatile way to organize and manage data, especially when dealing with complex entities that possess various attributes.

  1. List of Objects
    Definition:
    A list of objects is a collection where each item in the list is an object. This structure allows for the organization of multiple entities, each having its own set of properties defined as key-value pairs.

    Characteristics:

    • Combines Lists and Objects: Each item in the list is an object, which can have multiple attributes.

    • Ordered Collection: The order of objects in the list is maintained.

    • Mutable: You can add, remove, or modify objects within the list.

Apply the concepts of list, object, and list of objects

  1. Product Table
IDNAMECATEGORYPRICESTOCKSUPPLIER EMAIL
1LAPTOPELECTRONICS75050supplier@gmail.com
2DESK CHAIRFURNITURE100200supplier@gmail.com
3SMART WATCHELECTRONICS200150supplier@gmail.com
4NOTEBOOKSTATIONARY5500supplier@gmail.com
5RUNNING SHOESAPPAREL80100supplier@gmail.com
product_name_list = ["Laptop", "Desk Chair", "Smartwatch", "Notebook", "Running Shoes"]
product_category_list = ["Electronics", "Furniture", "Electronics", "Stationery", "Apparel"]
product_id_list = [1, 2, 3, 4, 5]
product_price_list = [750, 100, 200, 5, 80]
product_stock_list = [50, 200, 150, 500, 100]
product_supplier_email_list = ["supplier1@gmail.com", "supplier2@gmail.com", "supplier3@gmail.com", "supplier4@gmail.com", "supplier5@gmail.com"]

Object

An object combines several properties of a single entity, such as ID, Name, Category, Price, Stock, and Supplier Email. For example, an object can represent one product, encapsulating all its relevant details in one structure.

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"
}

List of Objects

A list of objects is a structure that merges the concepts of lists and objects. In this case, each object in the list represents a distinct entity, such as a product, and the list itself contains multiple of these objects. This allows for organized storage and easy access to various entities and their associated properties.

products = [
    {
        "id": 1,
        "name": "Laptop",
        "category": "Electronics",
        "price": 750,
        "stock": 50,
        "supplier_email": "supplier1@gmail.com"
    },
    {
        "id": 2,
        "name": "Desk Chair",
        "category": "Furniture",
        "price": 100,
        "stock": 200,
        "supplier_email": "supplier2@gmail.com"
    },
    {
        "id": 3,
        "name": "Smartwatch",
        "category": "Electronics",
        "price": 200,
        "stock": 150,
        "supplier_email": "supplier3@gmail.com"
    },
    {
        "id": 4,
        "name": "Notebook",
        "category": "Stationery",
        "price": 5,
        "stock": 500,
        "supplier_email": "supplier4@gmail.com"
    },
    {
        "id": 5,
        "name": "Running Shoes",
        "category": "Apparel",
        "price": 80,
        "stock": 100,
        "supplier_email": "supplier5@gmail.com"
    }
]
  1. Employee Table
IDNameDepartmentAgeEmail
1John DoeSales30john.doe@company.com
2Jane SmithHuman Resources25jane.smith@company.com
3Mark JohnsonIT40mark.johnson@company.com
4Lisa WongMarketing28lisa.wong@company.com
5Paul McDonaldFinance35paul.mcdonald@company.com

LIST

employee_name_list = ["John Doe", "Jane Smith", "Mark Johnson", "Lisa Wong", "Paul McDonald"]
employee_department_list = ["Sales", "Human Resources", "IT", "Marketing", "Finance"]
employee_id_list = [1, 2, 3, 4, 5]
employee_age_list = [30, 25, 40, 28, 35]
employee_email_list = ["john.doe@company.com", "jane.smith@company.com", "mark.johnson@company.com", "lisa.wong@company.com", "paul.mcdonald@company.com"]

OBJECT

employee_object1 = {
    "id": 1,
    "name": "John Doe",
    "department": "Sales",
    "age": 30,
    "email": "john.doe@company.com"
}

employee_object2 = {
    "id": 2,
    "name": "Jane Smith",
    "department": "Human Resources",
    "age": 25,
    "email": "jane.smith@company.com"
}

employee_object3 = {
    "id": 3,
    "name": "Mark Johnson",
    "department": "IT",
    "age": 40,
    "email": "mark.johnson@company.com"
}

employee_object4 = {
    "id": 4,
    "name": "Lisa Wong",
    "department": "Marketing",
    "age": 28,
    "email": "lisa.wong@company.com"
}

employee_object5 = {
    "id": 5,
    "name": "Paul McDonald",
    "department": "Finance",
    "age": 35,
    "email": "paul.mcdonald@company.com"
}

LIST OF OBJECTS

employees = [
    {
        "id": 1,
        "name": "John Doe",
        "department": "Sales",
        "age": 30,
        "email": "john.doe@company.com"
    },
    {
        "id": 2,
        "name": "Jane Smith",
        "department": "Human Resources",
        "age": 25,
        "email": "jane.smith@company.com"
    },
    {
        "id": 3,
        "name": "Mark Johnson",
        "department": "IT",
        "age": 40,
        "email": "mark.johnson@company.com"
    },
    {
        "id": 4,
        "name": "Lisa Wong",
        "department": "Marketing",
        "age": 28,
        "email": "lisa.wong@company.com"
    },
    {
        "id": 5,
        "name": "Paul McDonald",
        "department": "Finance",
        "age": 35,
        "email": "paul.mcdonald@company.com"
    }
]
  1. Book’s Table
IDTitleAuthorGenrePublished YearISBN
1The Great GatsbyF. Scott FitzgeraldFiction1925978-0743273565
2To Kill a MockingbirdHarper LeeFiction1960978-0060935467
31984George OrwellDystopian1949978-0451524935
4The Catcher in the RyeJ.D. SalingerFiction1951978-0316769488
5A Brief History of TimeStephen HawkingNon-Fiction1988978-0553380163

LIST

book_id_list = [1, 2, 3, 4, 5]
book_title_list = ["The Great Gatsby", "To Kill a Mockingbird", "1984", "The Catcher in the Rye", "A Brief History of Time"]
book_author_list = ["F. Scott Fitzgerald", "Harper Lee", "George Orwell", "J.D. Salinger", "Stephen Hawking"]
book_genre_list = ["Fiction", "Fiction", "Dystopian", "Fiction", "Non-fiction"]
book_published_year_list = [1925, 1960, 1949, 1951, 1988]
book_isbn_list = ["978-0743273565", "978-0060935467", "978-0451524935", "978-0316769488", "978-0553380163"]
book_stock_list = [20, 35, 40, 25, 10]
book_price_list = [15.99, 10.99, 9.99, 8.99, 18.99]

OBJECT

book1 = {
    "id": 1,
    "title": "The Great Gatsby",
    "author": "F. Scott Fitzgerald",
    "genre": "Fiction",
    "published_year": 1925,
    "isbn": "978-0743273565",
    "stock": 20,
    "price": 15.99
}

book2 = {
    "id": 2,
    "title": "To Kill a Mockingbird",
    "author": "Harper Lee",
    "genre": "Fiction",
    "published_year": 1960,
    "isbn": "978-0060935467",
    "stock": 35,
    "price": 10.99
}

book3 = {
    "id": 3,
    "title": "1984",
    "author": "George Orwell",
    "genre": "Dystopian",
    "published_year": 1949,
    "isbn": "978-0451524935",
    "stock": 40,
    "price": 9.99
}

book4 = {
    "id": 4,
    "title": "The Catcher in the Rye",
    "author": "J.D. Salinger",
    "genre": "Fiction",
    "published_year": 1951,
    "isbn": "978-0316769488",
    "stock": 25,
    "price": 8.99
}

book5 = {
    "id": 5,
    "title": "A Brief History of Time",
    "author": "Stephen Hawking",
    "genre": "Non-fiction",
    "published_year": 1988,
    "isbn": "978-0553380163",
    "stock": 10,
    "price": 18.99
}

LIST OF OBJECTS

books = [
    {
        "id": 1,
        "title": "The Great Gatsby",
        "author": "F. Scott Fitzgerald",
        "genre": "Fiction",
        "published_year": 1925,
        "isbn": "978-0743273565",
        "stock": 20,
        "price": 15.99
    },
    {
        "id": 2,
        "title": "To Kill a Mockingbird",
        "author": "Harper Lee",
        "genre": "Fiction",
        "published_year": 1960,
        "isbn": "978-0060935467",
        "stock": 35,
        "price": 10.99
    },
    {
        "id": 3,
        "title": "1984",
        "author": "George Orwell",
        "genre": "Dystopian",
        "published_year": 1949,
        "isbn": "978-0451524935",
        "stock": 40,
        "price": 9.99
    },
    {
        "id": 4,
        "title": "The Catcher in the Rye",
        "author": "J.D. Salinger",
        "genre": "Fiction",
        "published_year": 1951,
        "isbn": "978-0316769488",
        "stock": 25,
        "price": 8.99
    },
    {
        "id": 5,
        "title": "A Brief History of Time",
        "author": "Stephen Hawking",
        "genre": "Non-fiction",
        "published_year": 1988,
        "isbn": "978-0553380163",
        "stock": 10,
        "price": 18.99
    }
]
  1. University’s Table
IDNameLocationEstablished YearTypeWebsite
1University of the PhilippinesQuezon City1908Publicup.edu.ph
2Ateneo de Manila UniversityQuezon City1859Privateateneo.edu
3De La Salle UniversityManila1911Privatedlsu.edu.ph
4University of Santo TomasManila1611Privateust.edu.ph
5Polytechnic University of the PhilippinesManila1904Publicpup.edu.ph

LIST

university_id_list = [1, 2, 3, 4, 5]
university_name_list = ["University of the Philippines", "Ateneo de Manila University", "De La Salle University", "University of Santo Tomas", "Polytechnic University of the Philippines"]
university_location_list = ["Quezon City", "Quezon City", "Manila", "Manila", "Manila"]
university_established_year_list = [1908, 1859, 1911, 1611, 1904]
university_type_list = ["Public", "Private", "Private", "Private", "Public"]
university_website_list = ["www.up.edu.ph", "www.ateneo.edu", "www.dlsu.edu.ph", "www.ust.edu.ph", "www.pup.edu.ph"]

OBJECT

university1 = {
    "id": 1,
    "name": "University of the Philippines",
    "location": "Quezon City",
    "established_year": 1908,
    "type": "Public",
    "website": "www.up.edu.ph"
}

university2 = {
    "id": 2,
    "name": "Ateneo de Manila University",
    "location": "Quezon City",
    "established_year": 1859,
    "type": "Private",
    "website": "www.ateneo.edu"
}

university3 = {
    "id": 3,
    "name": "De La Salle University",
    "location": "Manila",
    "established_year": 1911,
    "type": "Private",
    "website": "www.dlsu.edu.ph"
}

university4 = {
    "id": 4,
    "name": "University of Santo Tomas",
    "location": "Manila",
    "established_year": 1611,
    "type": "Private",
    "website": "www.ust.edu.ph"
}

university5 = {
    "id": 5,
    "name": "Polytechnic University of the Philippines",
    "location": "Manila",
    "established_year": 1904,
    "type": "Public",
    "website": "www.pup.edu.ph"
}

LIST OF OBJECTS

universities = [
    {
        "id": 1,
        "name": "University of the Philippines",
        "location": "Quezon City",
        "established_year": 1908,
        "type": "Public",
        "website": "www.up.edu.ph"
    },
    {
        "id": 2,
        "name": "Ateneo de Manila University",
        "location": "Quezon City",
        "established_year": 1859,
        "type": "Private",
        "website": "www.ateneo.edu"
    },
    {
        "id": 3,
        "name": "De La Salle University",
        "location": "Manila",
        "established_year": 1911,
        "type": "Private",
        "website": "www.dlsu.edu.ph"
    },
    {
        "id": 4,
        "name": "University of Santo Tomas",
        "location": "Manila",
        "established_year": 1611,
        "type": "Private",
        "website": "www.ust.edu.ph"
    },
    {
        "id": 5,
        "name": "Polytechnic University of the Philippines",
        "location": "Manila",
        "established_year": 1904,
        "type": "Public",
        "website": "www.pup.edu.ph"
    }
]
  1. Restaurant’s Table
IDNameLocationCuisine TypeEstablished YearWebsite or Contact
1Vikings Luxury BuffetPasay CityBuffet2011[**

vikings.ph](http://www.vikings.ph/) | | 2 | Antoni’s Restaurant | Tagaytay City | Fine Dining | 2002 | [antoniosrestaurant.ph](http://antoniosrestaurant.ph) | | 3 | Mesa Filipino Moderne | Makati City | Filipino | 2009 | [mesa.ph](http://www.mesa.ph/) | | 4 | Manam Comfort Filipino | Quezon City | Filipino | 2013 | [manam.ph](http://www.manam.ph/) | | 5 | Ramen Nagi | Various Locations | Japanese | 2013 | [ramennagi.com.ph**](http://www.ramennagi.com.ph/) |

LIST

restaurant_id_list = [1, 2, 3, 4, 5]
restaurant_name_list = ["Vikings Luxury Buffet", "Antonio's Restaurant", "Mesa Filipino Moderne", "Manam Comfort Filipino", "Ramen Nagi"]
restaurant_location_list = ["Pasay City", "Tagaytay", "Makati City", "Quezon City", "Various Locations"]
restaurant_cuisine_type_list = ["Buffet", "Fine Dining", "Filipino", "Filipino", "Japanese"]
restaurant_established_year_list = [2011, 2002, 2009, 2013, 2013]
restaurant_website_list = ["www.vikings.ph", "www.antoniosrestaurant.ph", "www.mesa.ph", "www.manam.ph", "www.ramennagi.com.ph"]

OBJECT

restaurant1 = {
    "id": 1,
    "name": "Vikings Luxury Buffet",
    "location": "Pasay City",
    "cuisine_type": "Buffet",
    "established_year": 2011,
    "website": "www.vikings.ph"
}

restaurant2 = {
    "id": 2,
    "name": "Antonio's Restaurant",
    "location": "Tagaytay",
    "cuisine_type": "Fine Dining",
    "established_year": 2002,
    "website": "www.antoniosrestaurant.ph"
}

restaurant3 = {
    "id": 3,
    "name": "Mesa Filipino Moderne",
    "location": "Makati City",
    "cuisine_type": "Filipino",
    "established_year": 2009,
    "website": "www.mesa.ph"
}

restaurant4 = {
    "id": 4,
    "name": "Manam Comfort Filipino",
    "location": "Quezon City",
    "cuisine_type": "Filipino",
    "established_year": 2013,
    "website": "www.manam.ph"
}

restaurant5 = {
    "id": 5,
    "name": "Ramen Nagi",
    "location": "Various Locations",
    "cuisine_type": "Japanese",
    "established_year": 2013,
    "website": "www.ramennagi.com.ph"
}

LIST OF OBJECTS

restaurants = [
    {
        "id": 1,
        "name": "Vikings Luxury Buffet",
        "location": "Pasay City",
        "cuisine_type": "Buffet",
        "established_year": 2011,
        "website": "www.vikings.ph"
    },
    {
        "id": 2,
        "name": "Antonio's Restaurant",
        "location": "Tagaytay",
        "cuisine_type": "Fine Dining",
        "established_year": 2002,
        "website": "www.antoniosrestaurant.ph"
    },
    {
        "id": 3,
        "name": "Mesa Filipino Moderne",
        "location": "Makati City",
        "cuisine_type": "Filipino",
        "established_year": 2009,
        "website": "www.mesa.ph"
    },
    {
        "id": 4,
        "name": "Manam Comfort Filipino",
        "location": "Quezon City",
        "cuisine_type": "Filipino",
        "established_year": 2013,
        "website": "www.manam.ph"
    },
    {
        "id": 5,
        "name": "Ramen Nagi",
        "location": "Various Locations",
        "cuisine_type": "Japanese",
        "established_year": 2013,
        "website": "www.ramennagi.com.ph"
    }
]

Why Are Data Structures Important?

Understanding data structures, such as lists and objects, is fundamental in programming because they provide efficient ways to organize, access, and manipulate data. Proper use of data structures leads to faster algorithms, optimized performance, and more scalable applications, which is crucial in software development.

Frontend and Backend Development Context

  • Frontend: Data structures are essential when dynamically displaying information on a webpage, such as student profiles or product lists. They allow the efficient rendering and interaction of data for users.

  • Backend: When handling data behind the scenes, such as processing requests or storing information before sending it to the frontend or a database, data structures are key to efficiently managing this information.

The Importance of Data Structures

  1. Efficient Data Management:
    Data structures allow for the easy storage, access, and modification of data. Lists (arrays) make it simple to iterate over items, while objects (dictionaries) enable quick retrieval of data using keys.

  2. Improved Performance:
    The choice of data structure has a direct impact on performance. For example, searching through a list can take time, but using a hash table (a type of object) speeds up lookups to constant time (O(1)), making applications more responsive.

  3. Real-World Application:
    Data structures are used in various real-world scenarios. For example, they manage user profiles, product listings, and other structured data in web development. Understanding lists and objects helps developers create interactive, dynamic web applications.

  4. Foundation for Advanced Concepts:
    Learning basic data structures like arrays, linked lists, and trees lays the groundwork for understanding more advanced structures and algorithms. This knowledge is crucial for improving performance, solving complex problems, and optimizing code.

Conclusion

Data structures are not just theoretical concepts; they are essential tools in practical software development. Mastering them allows developers to create applications that are faster, more responsive, easier to maintain, and scalable. Whether you're working on a small project or building a large platform, a solid understanding of data structures is critical for success in the programming world.

More from this blog

thelastweaboo

20 posts