Master Python Dictionaries
Cañete,Brandon L.
5 min read
Table of contents
Product Dictionary Code
# Product 1
product_object_one = {
"product_id" : 1,
"product_name" : "Kopiko Black 3 in 1 Twin Pack",
"product_type" : "Coffee",
"product_price" : "12.00",
"product_net_weight" : "60 g",
}
# Product 2
product_object_two = {
"product_id" : 2,
"product_name" : "Piattos Sour Cream & Onion",
"product_type" : "Chips",
"product_price" : "45.00",
"product_net_weight" : "85 g"
}
# Product 3
product_object_three = {
"product_id" : 3,
"product_name" : "Coke Original",
"product_type" : "Soft Drink",
"product_price" : "125.00",
"product_net_weight" : "1.5 L"
}
# Product 4
product_object_four = {
"product_id" : 4,
"product_name" : "Golden Fiesta Cooking Oil",
"product_type" : "Lipids",
"product_price" : "72.25",
"product_net_weight" : "485 ml"
}
# Product 5
product_object_five = {
"product_id" : 5,
"product_name" : "Tender Juicy Hotdog Regular",
"product_type" : "Sausage",
"product_price" : "226.70",
"product_net_weight" : "1 kg"
}
# Hold the product dictionaries to products variables
products = [product_object_one,product_object_two,product_object_three,product_object_four,product_object_five]
# Loop through the dictionaries
for product in products:
# Print the data
print(f"Product Name: {product.get('product_name')}, Product Type : {product.get('product_type')}, Product Price : {product.get('product_price')}, Product Net Weight : {product.get('product_net_weight')}")
Product Dictionary Output
Employee Dictionary Code
# Employee 1
employee_object_one = {
"employee_id" : 1,
"employee_name" : "Arthur Morgan",
"employee_job_title" : "Designer",
"employee_department" : "IT",
"employee_salary" : "20,000.00",
"employee_schedule_work" : "Monday to Friday"
}
# Employee 2
employee_object_two = {
"employee_id" : 2,
"employee_name" : "Henry Goose",
"employee_job_title" : "Developer",
"employee_department" : "IT",
"employee_salary" : "30,000.00",
"employee_schedule_work" : "Monday to Saturday"
}
# Employee 3
employee_object_three = {
"employee_id" : 3,
"employee_name" : "Jeffrey Morrey",
"employee_job_title" : "Advertiser",
"employee_department" : "HR",
"employee_salary" : "25,000.00",
"employee_schedule_work" : "Monday to Thursday"
}
# Employee 4
employee_object_four = {
"employee_id" : 4,
"employee_name" : "Godfrey Huther",
"employee_job_title" : "Engineer",
"employee_department" : "Construction",
"employee_salary" : "35,000.00",
"employee_schedule_work" : "Monday to Friday"
}
# Employee 5
employee_object_five = {
"employee_id" : 5,
"employee_name" : "Angeline Grace",
"employee_job_title" : "Secretary",
"employee_department" : "HR",
"employee_salary" : "33,000.00",
"employee_schedule_work" : "Monday to Saturday"
}
# Hold the employee dictionaries to employees variable
employees = [employee_object_one,employee_object_two,employee_object_three,employee_object_four,employee_object_five]
# Loop through the dictionaries
for employee in employees:
# Print the data
print(f"Employee Name: {employee.get('employee_name')}, Employee Job Title: {employee.get('employee_job_title')}, Employee Department: {employee.get('employee_department')}, Employee Salary : {employee.get('employee_salary')}, Employee Schedule Work: {employee.get('employee_schedule_work')}")
Employee Dictionary Output
Books Dictionary Code
# Book 1
book_object_one = {
"book_id" : 1,
"book_title" : "GO Programming in easy steps",
"book_author" : "Mike McGrath",
"book_genre" : "Paperback · Non-fiction · Computer",
"book_price" : "839.00"
}
# Book 2
book_object_two = {
"book_id" : 2,
"book_title" : "Web Coding & Development All-in-one For Dummies",
"book_author" : "Paul McFedries",
"book_genre" : "Non-fiction · Computer",
"book_price" : "2,530.40"
}
# Book 3
book_object_three = {
"book_id" : 3,
"book_title" : "Effective Java",
"book_author" : "Joshua Bloch",
"book_genre" : "Paperback · Non-fiction · Business/Economics",
"book_price" : "1,149.74"
}
# Book 4
book_object_four = {
"book_id" : 4,
"book_title" : "Logic Primer, third edition",
"book_author" : "Colin Allen and Michael Hand",
"book_genre" : "Paperback · Non-fiction · Mathematics",
"book_price" : "1,919.32"
}
# Book 5
book_object_five = {
"book_id" : 5,
"book_title" : "Logically Fallacious",
"book_author" : "Bo Bennett",
"book_genre" : "Paperback · Non-fiction · Education",
"book_price" : "1,441.51"
}
# Hold the book dictionaries to boos variable
books = [book_object_one,book_object_two,book_object_three,book_object_four,book_object_five]
# Loop through the dictionaries
for book in books:
# Print the data
print(f"Book Title: {book.get('book_title')}, Book Author: {book.get('book_author')}, Book Genre: {book.get('book_genre')}, Book Price: {book.get('book_price')}")
Book Dictionary Output
University Dictionary Code
# University 1
university_object_one = {
"university_id" : 1,
"university_name" : "University of the Philippines",
"university_location" : "Diliman, Quezon City",
"university_size" : "493 hectares",
"university_founded_date" : "February 12, 1949",
"university_campus" : "Suburban",
}
# University 2
university_object_two = {
"university_id" : 2,
"university_name" : "Ateneo de Manila University",
"university_location" : "Katipunan Ave, Quezon City",
"university_size" : "83 hectares",
"university_founded_date" : "December 10, 1859",
"university_campus" : "Urban"
}
# University 3
university_object_three = {
"university_id" : 3,
"university_name" : "University of Santo Tomas",
"university_location" : "España Blvd, Sampaloc, Manila",
"university_size" : "21.5 hectares",
"university_founded_date" : "April 28, 1611",
"university_campus" : "Urban"
}
# University 4
university_object_four = {
"university_id" : 4,
"university_name" : "De La Salle University",
"university_location" : "Taft Ave, Malate, Manila",
"university_size" : "5.4 hectares",
"university_founded_date" : "June 16, 1911",
"university_campus" : "Urban"
}
# University 5
university_object_five = {
"university_id" : 5,
"university_name" : "Mapúa University",
"university_location" : "Muralla St, Intramuros, Manila",
"university_size" : "1.79 hectares",
"university_founded_date" : "January 25, 1925",
"university_campus" : "Urban"
}
# Hold the university dictionaries to universities variables
universities = [university_object_one,university_object_two,university_object_three,university_object_four,university_object_five]
# Loop through the dictionaries
for university in universities:
# Print the data
print(f"University Name: {university.get('university_name')}, University Location: {university.get('university_location')}, University Size: {university.get('university_size')}, University Founded Date: {university.get('university_founded_date')}, University Campus: {university.get('university_campus')}")
University Dictionary Output
Restaurant Dictionary Code
# Restaurant 1
restaurant_object_one = {
"restaurant_id" : 1,
"restaurant_name" : "Manila Bay Kitchen",
"restaurant_cuisine" : "Filipino, Bar, Pizza, Seafood, International",
"restaurant_location" : "Sheraton Manila Bay, Manila"
}
# Restaurant 2
restaurant_object_two = {
"restaurant_id" : 2,
"restaurant_name" : "Cafe Ilang-Ilang",
"restaurant_cuisine" : "Local cuisine, Italian, Chinese, Japanese, American, Filipino",
"restaurant_location" : "One Rizal Park Manila Hotel"
}
# Restaurant 3
restaurant_object_three = {
"restaurant_id" : 3,
"restaurant_name" : "The Aristocrat Restaurant",
"restaurant_cuisine" : "Filipino",
"restaurant_location" : "San Andres Street, Manila"
}
# Restaurant 4
restaurant_object_four = {
"restaurant_id" : 4,
"restaurant_name" : "Estoria Manila",
"restaurant_cuisine" : "Filipino, Bar, Cafe, Spanish, Asian",
"restaurant_location" : "Ermita Ermita Plaza, Manila"
}
# Restaurant 5
restaurant_object_five = {
"restaurant_id" : 5,
"restaurant_name" : "Barbara's Heritage Restaurant",
"restaurant_cuisine" : "Filipino, International, Asian",
"restaurant_location" : "Intramuros, Manila"
}
# Hold the restaurant dictionaries to restaurants variable
restaurants = [restaurant_object_one,restaurant_object_two,restaurant_object_three,restaurant_object_four,restaurant_object_five]
# Loop through the dictionaries
for restaurant in restaurants:
# Print the data
print(f"Restaurant Name: {restaurant.get('restaurant_name')}, Restaurant Cuisine: {restaurant.get('restaurant_cuisine')}, Restaurant Location: {restaurant.get('restaurant_location')}")
Restaurant Dictionary Output
What I learned
I learned that dictionaries is really good when storing data because it has key and it is also good in terms of accessing the data because you can use it’s key to get the value pair or the data from it’s key.
Overall, dictionaries is one of the most important data structure in python because of it’s simplicity to get the data from it and also because of it’s readability.
Github Repository Link
https://github.com/BrandonLCanete/MasterPythonDictionaries.git
0
Subscribe to my newsletter
Read articles from Cañete,Brandon L. directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by