Data type optimization
📖 Scenario: You work as a data analyst for a retail company. You have a dataset of product sales with product IDs, quantities sold, and prices. The dataset is large, and you want to optimize the data types to save memory and improve performance.
🎯 Goal: You will create a dictionary with product sales data, add a configuration for the maximum quantity threshold, optimize the data types by converting values to smaller types where possible, and finally print the optimized data.
📋 What You'll Learn
Create a dictionary called
sales_data with exact entries for product IDs as strings and their quantities and prices as integers and floats.Create a variable called
max_quantity set to 1000.Use a dictionary comprehension to create a new dictionary called
optimized_sales where quantities greater than max_quantity are converted to float and others to int, and prices are converted to float with two decimals.Print the
optimized_sales dictionary.💡 Why This Matters
🌍 Real World
Optimizing data types helps reduce memory use and speeds up data processing in real-world data analysis tasks.
💼 Career
Data analysts and scientists often optimize data types to handle large datasets efficiently and improve performance.
Progress0 / 4 steps