Group Items by Category Practice Problem
This data science coding problem helps you practice Data Structures, group items by category, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Data Structures.
- Problem ID: 87
- Problem key: 87-group-items-by-category
- URL: https://datacrack.app/solve/87-group-items-by-category
- Difficulty: easy
- Topic: Data Structures
- Module: Python Fundamentals
Problem Statement
# 🧩 Group Items by Category
---
### 🎯 Goal
Grouping records by a shared attribute is the foundation of **aggregation** in data science. Before you use `df.groupby()`, you should understand how it works at the Python level: build a dictionary where each key is a category and each value is a list of items belonging to that category.
---
### 🔍 The Pattern
Given a list of records (each a dict with `"name"` and `"category"`), produce a dictionary that groups names under their category:
```
Input: [
{"name": "apple", "category": "fruit"},
{"name": "carrot", "category": "vegetable"},
{"name": "banana", "category": "fruit"}
]
Output: {
"fruit": ["apple", "banana"],
"vegetable": ["carrot"]
}
```
---
### 💻 Task
Implement `group_by_category(items)`.
---
### 📥 Input
- `items`: list of dicts, each with `"name"` (str) and `"category"` (str)
### 📤 Output
- dict mapping each category name to a list of item names in that category (in original order)
---
### 🧩 Starter Code
```python
def group_by_category(items):
"""
Group item names by their category.
Args:
items (list[dict]): Each dict has "name" and "category" keys
Returns:
dict: {category: [name, ...]} grouped by category
"""
groups = {}
for item in items:
# 🧠 TODO: Get the category and name from each item dict
# 🧠 TODO: If category not in groups, initialize it as an empty list
# 🧠 TODO: Append the name to groups[category]
pass
return groups
```
---
### 💡 Example
```python
items = [
{"name": "Python", "category": "language"},
{"name": "R", "category": "language"},
{"name": "TensorFlow", "category": "library"}
]
group_by_category(items)
# Expected: {"language": ["Python", "R"], "library": ["TensorFlow"]}
```
---
### 🔑 Key Concepts
- `dict.setdefault(key, default)` — initializes a key if it doesn't exist
- Alternatively: check `if key not in dict` then set `dict[key] = []`
- This is the exact logic behind `df.groupby('category')['name'].apply(list)`Starter Code
def group_by_category(items):
"""
Group item names by their category.
Args:
items (list[dict]): Each dict has "name" and "category" keys
Returns:
dict: {category: [name, ...]} grouped by category
"""
groups = {}
for item in items:
# 🧠 TODO: Get the category and name from each item dict
# 🧠 TODO: If category not in groups, initialize it as an empty list
# 🧠 TODO: Append the name to groups[category]
pass
return groups